在活动被杀死时销毁未绑定的服务

时间:2015-03-27 04:33:24

标签: android android-layout android-activity service

我想创建一个即使在应用程序被杀死时也在运行的服务,因此我创建了一个未绑定的服务,以便它不会绑定到活动生命周期。但每次我通过长按和滑动杀死应用程序,该服务也会被杀死。有些人可以帮助我。 感谢

服务

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);
    }

} 

我从这样的活动中调用它

startService(new Intent(MyService.class.getName()));

该服务运行良好,直到应用程序处于后台或前台,但是当我长时间 按下并清除应用程序,服务也会停止,并显示崩溃消息,如此

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

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

2 个答案:

答案 0 :(得分:2)

// 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());
    }

答案 1 :(得分:0)

除了一个改变之外,你做的一切都很正确:

您应该从代码中删除绑定重写方法:

删除它:

    @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*/);