简体   繁体   English

Android:如何从非活动类中调用服务时知道服务是否已完成执行

[英]Android : How to know if service has finished execution when called from a non-activity class

  1. I have Push Notification receiver class 我有推送通知接收器类
  2. Am calling LocationService , a service to check for me the location of user and calculate if user is within 1 km radius, and updates a variable in shared pref. 我正在调用LocationService,该服务可为我检查用户的位置并计算用户是否在1 km半径内,并更新共享首选项中的变量。
  3. I display the notification 我显示通知

Question : How do I know service has finished execution ? 问题:我如何知道服务已完成执行? Please note that I can not use bindservice() as service is called from a non activity class 请注意,我不能使用bindservice(),因为服务是从非活动类中调用的

public class PushReceiver extends ParsePushBroadcastReceiver  {
    public   static Context mContext;

        @Override
        public void onPushReceive(Context context , Intent intent) {
            mContext = MyApp.getContext();
            //startService(new Intent(this, LocationService.class));
            Intent i1 = new Intent (context, LocationService.class);
            context.startService(i1);

// here I want to check if LocationService has finished execution
 if (1 == 1){
           super.onPushReceive(context, intent);
            }
        }// end of onPushRecieve

}

Just fire off your notification inside your custom LocationService . 只需在自定义LocationService触发您的通知即可。

Plus, you don't want to do anything async inside a BroadcastReceiver as it will exit before the async method performs any callback. 另外,您不希望在BroadcastReceiver内部执行任何异步操作,因为它将在async方法执行任何回调之前退出。

See BroadcastReceiver 请参阅BroadcastReceiver

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。 Once your code returns from this function, the system considers the object to be finished and no longer active. 一旦您的代码从该函数返回,系统将认为该对象已完成并且不再处于活动状态。

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes. 这对您在onReceive(Context,Intent)实现中可以执行的操作具有重要影响:要求异步操作的任何内容都不可用,因为您需要从函数中返回以处理异步操作,但是到那时,BroadcastReceiver是不再活动,因此系统可以在异步操作完成之前终止其进程。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. 特别是,您可能不会显示对话框或从BroadcastReceiver内部绑定到服务。 For the former, you should instead use the NotificationManager API. 对于前者,您应该改用NotificationManager API。 For the latter, you can use Context.startService() to send a command to the service.A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). 对于后者,您可以使用Context.startService()向服务发送命令.BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。 Once your code returns from this function, the system considers the object to be finished and no longer active. 一旦您的代码从该函数返回,系统将认为该对象已完成并且不再处于活动状态。

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes. 这对您在onReceive(Context,Intent)实现中可以执行的操作具有重要影响:要求异步操作的任何内容都不可用,因为您需要从函数中返回以处理异步操作,但是到那时,BroadcastReceiver是不再活动,因此系统可以在异步操作完成之前终止其进程。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. 特别是,您可能不会显示对话框或从BroadcastReceiver内部绑定到服务。 For the former, you should instead use the NotificationManager API. 对于前者,您应该改用NotificationManager API。 For the latter, you can use Context.startService() to send a command to the service. 对于后者,可以使用Context.startService()将命令发送到服务。

In the event that you have no control over the custom LocationService code then you should probably just start your own Service and poll for the shared pref value your waiting for. 如果您无法控制自定义LocationService代码,则可能应该只启动自己的Service并轮询等待的共享pref值。 This is however not ideal. 然而,这不是理想的。

I notice the Parse receiver you are extending does not extend WakefulBroadcastReceiver which may be an issue - check out the docs for this... 我注意到您正在扩展的Parse接收器不会扩展WakefulBroadcastReceiver ,这可能是一个问题-请为此查看文档...

Thanks Dori It worked. 谢谢Dori它的工作。 LocationService is my own class . LocationService是我自己的类。 I trapped the Notfication message in PushReciever and displayed it in the Service. 我在PushReciever中捕获了通知消息,并将其显示在服务中。

public class PushReceiver extends ParsePushBroadcastReceiver  {
    public   static Context pushContext;
    public static Notification pushNoti;
        @Override
        public void onPushReceive(Context context , Intent intent) {

            pushContext = context;
            Intent i1 = new Intent (context, LocationService.class);
            pushNoti= getNotification(context,intent);
            context.startService(i1);

        }// end of onPushRecieve
}

// and in LocationService Service just fired it


    NotificationManager nm = (NotificationManager) 

    PushReceiver.pushContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);


    public void onConnected(Bundle bundle) {

            Log.d("onConnected ", "Value: ");
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            nm.notify(0,PushReceiver.pushNoti);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM