Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当应用程序转到后台时停止后台服务_Android_Background Service - Fatal编程技术网

Android 当应用程序转到后台时停止后台服务

Android 当应用程序转到后台时停止后台服务,android,background-service,Android,Background Service,我的android应用程序中有后台服务,我从MainActivity onResume()启动服务方法,它工作正常。但当用户按home按钮时,我如何停止服务。因为当前当用户按home按钮时,应用程序移动到后台,然后用户打开其他应用程序,一段时间后,调用我的服务方法并强制停止应用程序。下面是我启动服务的代码- Intent msgIntent = new Intent(mContext, MyBackgroundService.class); startService(msgInt

我的android应用程序中有后台服务,我从MainActivity onResume()启动服务方法,它工作正常。但当用户按home按钮时,我如何停止服务。因为当前当用户按home按钮时,应用程序移动到后台,然后用户打开其他应用程序,一段时间后,调用我的服务方法并强制停止应用程序。下面是我启动服务的代码-

Intent msgIntent = new Intent(mContext, MyBackgroundService.class);
        startService(msgIntent);
提前谢谢

已编辑

在我的服务中,我使用以下代码-

 public void callAsynchronousTask() {
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {       
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {       
                try {
                    callWebservice();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
};
timer.schedule(doAsynchronousTask, START_DELAY, DELAY);
 }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    callAsynchronousTask();
    return Service.START_NOT_STICKY;
}

@Override
public void onCreate() {
    mContext = this;
    super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(timer!=null){
        timer.cancel();
    }
    stopSelf();
}
在我的活动中,我使用以下代码停止服务-

@Override
protected void onStop() {
    try{
         stopService(new Intent(this, MyBackgroundService.class));
         isServiceRunning = false;
    }
   catch(Exception e){
    e.printStackTrace();
   }
    super.onStop();
}

@Override
protected void onPause() {
    try{
         stopService(new Intent(this, MyBackgroundService.class));
         isServiceRunning = false;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    super.onPause();
}

但我的服务是在我使用其他应用程序时运行的,它强制停止应用程序。我从后台服务调用一些Web服务,然后将服务响应存储在数据库中。

在onPause()和onStop()中停止服务


按Home按钮可导致暂停()函数。重写onPause()并调用stopService: mContext.stopService(新意图, MyBackgroundService.class)


上面的代码将持续检查用户是否按下了home按钮。

当我们打开其他应用程序时,我们的应用程序(位于后台)将从内存中清除,但是整个应用程序不会被删除,但一些不需要的数据和活动会被完成。 在您的情况下,当尝试更新UI时,将要更新的活动从内存和正在运行的后台服务中清除,然后抛出NullPointerException使其崩溃

因此,请在onCreate()中保存activty(其UI将被更新)的引用,并在finish()方法中将该引用设置为null,然后在后台服务中检查该引用,如果该引用不为null,则更新UI,否则不进行更新

// Global Class for saving the reference
class GlobalReference{
      public static <name_of_your_activity> activityRef;
}
在后台服务中

if( GlobalReference.activityRef != null){
    // update the UI of your activity
}
希望此代码能解决您的问题。
快乐编码…

我尝试了您的代码,但当按下“主页”按钮时,不会调用onKeyDown方法。您必须从后台服务更新ui,这可能会导致崩溃。如果是这样,您需要对活动的上下文进行检查,并在onPause和Onkey down方法中将其设为null,然后在更新ui之前对其进行检查。请检查更新的问题。因为我已尝试了您的代码,但在应用程序处于后台且其他应用程序正在运行时,应用程序仍会崩溃。
// Global Class for saving the reference
class GlobalReference{
      public static <name_of_your_activity> activityRef;
}
 onCreate(){
     GlobalReference.activityRef = this;
 }


finish(){
    GlobalReference.activityRef = null;
}
if( GlobalReference.activityRef != null){
    // update the UI of your activity
}