首页 > 解决方案 > android 使用 START_STICKY 启动前台服务

问题描述

我无法理解android关于服务的新特性。在谷歌文档中,oreo当应用程序在后台时,开发人员必须使用前台服务来启动服务。

我找到了这个描述。

'从Android O开始,如果你的应用程序在后台(检查以上三个条件),你的应用程序可以创建并运行几分钟的后台服务。

几分钟后,您的应用程序将进入空闲阶段。当您的应用程序进入空闲阶段时,系统将停止所有后台服务,就像您的服务调用一样Service.stopSelf()

我无法理解即使我使用 START_STICKY 启动服务,它不会重新启动吗?我知道如果我从 START_STICKY 开始,它会在 kill 后完全重新启动。为什么我必须JobScheduler用于某些需求(位置更新等)。有人可以解释一下吗。我不能很好地理解谷歌文档。

我现在在手机上测试它。我galaxy note 8 api26在应用程序启动时使用 startService 启动服务,并在关闭应用程序后重新启动。旧版本之间有什么区别

谢谢你。

标签: androidandroid-8.0-oreobackground-serviceforeground-service

解决方案


    public class MyActivity2 extends Activity {

    private Intent serviceIntent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        serviceIntent = new Intent(this, MyService.class);
        serviceIntent.putExtra("name", "ahmet vefa saruhan");
        startService(serviceIntent);
        findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(new Intent(getApplicationContext(),MyService.class));
            }
        });
    }
}
    public class MyService extends Service {

    private String myaction;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MYSERVICE STATUS: "," ONSTARTCOMMAND action : " + ((intent != null && intent.getStringExtra("name") != null) ? intent.getStringExtra("name") : "yok") + " OLD ACTION : "+(myaction != null ? myaction : "yok"));
        Log.d("MYTHREAD name : ",Thread.currentThread().getName());
        //intent.putExtra("name","isim değişti");
        myaction = (intent != null) ? intent.getAction() : null;
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
       Log.d("MYSERVICE STATUS: "," ONCREATED");
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground()
    {
        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d("MYSERVICE STATUS: "," onTaskRemoved");
        super.onTaskRemoved(rootIntent);
    }
}

推荐阅读