【问题标题】:Create a Android App Service which run even after app is terminated创建一个即使在应用程序终止后仍运行的 Android 应用程序服务
【发布时间】:2017-03-07 11:21:06
【问题描述】:

这是我想在后台运行的服务

public class CustomMyService extends Service {

     public CustomMyService() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY;
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent intent = new Intent("com.android.ServiceStopped");
        sendBroadcast(intent);
    }
}

清单文件

<service android:name=".CustomMyService">
            <intent-filter android:priority="1000">
                <action android:name="android.location.PROVIDERS_CHANGED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

请有人告诉我。我做了所有的谷歌和 youtube 搜索,没有任何工作。

我正在使用 redmi note 3,其中我有一个名为 autostart 的选项,如果我允许此应用程序自动启动,则服务将在后台运行。

但是很多其他安卓智能手机都没有这个选项,所以当应用终止时,应用服务会被杀死。

请让我知道服务如何运行,即使在应用程序终止后也是如此。

【问题讨论】:

  • 使用您希望服务执行的代码编写一个私有类方法,并从onStartCommand() 调用它。除非您需要一些东西来初始化服务,否则不需要构造函数。

标签: android android-service


【解决方案1】:

参见onStartCommand 方法和参数:

START_STICKY

START_NOT_STICKY

START_REDELIVER_INTENT

START_STICKY_COMPATIBILITY

如果不行,可能是你没有系统权限让服务在后台运行

【讨论】:

  • Facebook、whatsapp 之类的这些应用怎么一直在后台运行
【解决方案2】:

使用 AlarmManager 不时调用您的服务。因为应用程序终止时服务将停止。需要再次撤销,警报管理有助于重新启动您的服务。

有很多方法提到了持续服务,但是这些方法在kitkat版本之后没有帮助。

如果您在应用程序终止后成功地继续运行您的服务,而无需使用任何警报管理器等调度方法,请告诉我。

【讨论】:

  • Alarm Manager 我试过了,但它确实发生了..一旦我得到解决方案,我一定会分享..
【解决方案3】:

这里是演示:

 public class SyncJobService extends Service {
private String TAG = "mytag";
private Timer timer = new Timer();
private EventBus evenBus = EventBus.getDefault();
public static Date lastSyncDate;

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

@Override
public void onCreate() {
    super.onCreate();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            Log.d(TAG, "run: StartSyncEvent");
  //                Implement your logic


          }
        }, 0, 5 * 60 * 1000);//5 Minutes

}

@Override
public void onDestroy() {
    Log.d(TAG, "onDestroy: ");
    super.onDestroy();
}

}

ManiFest 文件:

    <service
        android:name=".service.SyncJobService"
        android:enabled="true"
        android:exported="false" />

【讨论】:

  • 用于与服务器同步数据。
【解决方案4】:

lenovo、xolo 等某些手机中,会终止后台第三方应用程序。因此,当应用程序关闭时,它不会允许后台服务。请使用其他手机检查您的服务。 1.更好的使用启动前台服务。 2.startsticky中,如果应用程序被终止,后台进程会自动重启服务。

【讨论】:

    【解决方案5】:

    将您的服务设置为前台进程,以便 Android 系统将其视为前台应用程序并且不会轻易杀死。使用以下代码将您的服务设为前台。

    StartForeground(100, new Notification ());
    

    请记住,您需要在 ondestroy of service 中调用 stopforeground(100)。

    【讨论】:

      【解决方案6】:
      猜你喜欢
      • 2021-04-24
      • 2023-03-02
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多