activity 被杀死时未绑定的服务被销毁

Unbound services destroyed while activity is killed

我想创建一个 运行 的服务,即使在应用程序被终止时也是如此,所以我创建了一个未绑定的服务,这样它就不会绑定到 activity 生命周期。但是每次我通过长按和滑动来终止应用程序时,该服务也会被终止。有人可以帮助我吗? 谢谢

服务

public class MyService extends Service {

    public GCMNotificationIntentService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Service binded");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "Service unbound");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "Service created");
        super.onCreate();
    }

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

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

        Log.d(TAG, "Service started");

        Thread readerThread = new Thread(new Runnable() {
            @Override
            public void run() {

                while(true) {
                    Log.d(TAG, "Thread present");
                    try {
                        // thread to sleep for 1000 milliseconds
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            }
        });
        readerThread.start();

        return super.onStartCommand(intent, flags, startId);
    }

} 

我是这样从 activity 调用它的

startService(新意图(MyService.class.getName()));

服务运行良好,直到应用程序处于后台或前台,但当我长时间 按住并扫一扫应用程序,服务也会停止并出现这样的崩溃消息

计划在 1000 毫秒内重启崩溃的服务 com.net.gs.MyService

I/ActivityManager( 1160): 强制停止服务 ServiceRecord{44989bf0 u0 com.net.gs.MyService}

你做对了所有事情,除了一个改变:

您应该从代码中删除 Binding 覆盖的方法:

删除这个:

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Service binded");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "Service unbound");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "Service created");
        super.onCreate();
    }

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

在此之后,您的问题应该会解决。

当您开始服务调用时:

startService(/* intent to service*/);
// Display sticky notification using below function in notification-bar.   
private static final int NOTIFICATION_ID=1;

 private void displayNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(
                "Player");
        builder.setContentText("content text");

        if (getFileStructure() != null) {
            String title = Utils.filter(getFileStructure().getTitle());
            if (title.length() > 45)
                title = title.substring(0, 44);
            builder.setContentText(Utils.filter(title));
        }
        Intent resultIntent = new Intent(PlayerService.this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                PlayerService.this, 0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        builder.setAutoCancel(false);
        NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(NOTIFICATION_ID, builder.build());
        startForeground(NOTIFICATION_ID, builder.build());
    }