繁体   English   中英

Android-如果应用程序终止,则远程服务也会终止

[英]Android - Remote service is terminated if application is terminated

Android SDK表示, 远程服务在应用程序的另一个进程中运行。 我认为这意味着如果该应用程序停止/终止...,则远程服务仍然保持运行状态。 但事实并非如此。

例如,我有这个远程服务:

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.os.IBinder;
import android.util.Log;

public class WatchDogService extends Service {

  private Timer timer = new Timer();

  @Override
  public void onCreate() {
    Log.i(WatchDogService.class.getName(), "WatchDog start");

    timer.scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        Log.i(WatchDogService.class.getName(), "WatchDog boo boo!!! ^^");
      }
    }, 0, 5000);
  }

  @Override
  public void onDestroy() {
    Log.i(WatchDogService.class.getName(), "WatchDog stop");
  }

  @Override
  public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(WatchDogService.class.getName(), "WatchDog has just been called...");
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
  }
}

AndroidManifest.xml:

<service
    android:name="WatchDogService"
    android:process=":remote" >
    <intent-filter>
        <action android:name="WatchDogService" />
    </intent-filter>
</service>

在另一项活动中,我将其称为:

startService(new Intent("WatchDogService"));

服务启动正常。 但是,如果我转到系统应用程序管理器,然后停止该应用程序,该服务也会终止。

即使应用程序终止,我也要保持服务运行。 我怎样才能做到这一点?

即使应用程序终止,我也要保持服务运行。 我怎样才能做到这一点?

你不知道 如果用户终止您的应用,则您的应用终止。 用户可以控制自己的Android设备,而不是您。

请尊重用户的意愿,并尝试不要构建用户将要强行停止,杀死任务或以其他方式摆脱的应用。

除了服务,您可以创建一个单独的应用程序并调用它,或者

除了此服务外,您还可以使用此链接中描述的AIDL创建单独的后台应用程序: http : //developer.android.com/guide/components/aidl.html

请注意,AIDL应该注册一个旨在启动原始应用程序的通知,并询问用户是否希望对其执行操作。

我希望“服务”表示服务,而不是“应用程序”。 我的意思是,我想要“服务经理”之类的东西。 但是我忘记了Android中的“应用程序”设置是针对最终用户的,而不是针对编码器的。

到目前为止,我已经知道了。

暂无
暂无

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

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