调用一个方法,而不是使用意向的活动 - Android开发者

问题描述:

我怎么能火在我的code的方法,而不是一个活动?

How can I fire a method instead of an Activity in my code?

我想用从的LocationManager AddProximityAlert()方法,但它需要一个意图工作,我不想调用另一个活动,因为我想火的方法是在同一个活动从我使用其中 AddProximityAlert()

I want to use the AddProximityAlert() method from the LocationManager but it needs an Intent to work and I don't want to call another activity since the method I want to fire is in the same Activity from where I'm using AddProximityAlert()

是这样的:

public clase onCreate()
{
......

LocationManager LM; // already initialized

LM.addProximityAlert(lat,long,radio,expiration,INTENT) <--- This INTENT needs to call myMethod()

}

public void MyMethod()
{

.......

}

我有几个小时,试图找到一个解决方案,所有我觉得是调用另一个活动,请尽可能多的具体的,即使我有做的的manifest.xml ,因为我以前没有使用过的意图。

I have several hours trying to find a solution for this, and all I find is for calling another activity, please be as much specific, even if I have to do something with the manifest.xml because I haven't used intents before.

感谢

如果你创建的Andr​​oid服务(或广播接收机可能更合适)处理的意图相同的应用程序中。

What if you create a android service (or broadcast receiver might be more appropriate) within the same application that handles the intent.

您不要离开相同的PID,这是一个非可视的服务与您永远不会离开你的活动?

You don't leave the same pid, it's a non-visual service and you never leave your activity?

您可以通过创建一个意图它的类名直接引用直接调用你的服务。

You can call your service directly by creating a intent the references it's class name directly.

       intent = new Intent(context, my_service.class);

然后,它会撞到服务onStartCommand()功能,你可以在那里做你的意图处理。

It then will hit the services "onStartCommand()" function and you can do your intent processing from there.