【问题标题】:Unbound services destroyed while activity is killed活动被杀死时未绑定的服务被破坏
【发布时间】:2015-05-31 08:22:44
【问题描述】:

我想创建一个即使在应用程序被终止时仍在运行的服务,所以我创建了一个未绑定的服务,以便它不绑定到活动生命周期。但是每次我通过长按和滑动来终止应用程序时,该服务也会被终止。可以请一些帮助我。 谢谢

服务

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}

【问题讨论】:

  • 当然,当你杀死一个进程时,在那个进程中运行的所有东西都会死掉。 Android 可能会(也可能不会)在新进程中重新启动某些组件。
  • 我已经解决了这个问题:stackoverflow.com/questions/16651009/…
  • 此外,我添加了一个函数 onDestroySubstitute(),我在取消绑定活动的 onDestroy 方法中的服务之前从 myActivity 调用该函数。在 onDestroySubstitute() 中,我使用例如准备服务以进行正确的重启。 SharedPreferences 并在那里保存我重启后需要的内容。

标签: android android-layout android-activity service


【解决方案1】:
// 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());
    }

【讨论】:

  • 如果 Android 需要内存来执行其他任务,它会终止任何服务。因此,如果您在通知栏中显示通知,则系统会将其假定为前台并破坏其他进程。但是android不能保证你的服务可以永远运行。
  • 我在 onStartCommand() 方法中调用 displayNotification() 方法但结果还是一样
  • 请将其添加到您的服务类的 OnCreate 中。调用上述函数后,确保通知显示在通知栏中。
  • 对不起,但结果相同....是的,通知正在显示,但是当我杀死应用程序时,通知也被隐藏了。并且服务被杀死并在 logcat 上提供与以前相同的输出
  • 能否请您在安卓模拟器或其他设备上进行测试。所以我们可以识别问题,xiomi定制了很多操作系统。
【解决方案2】:

你做的一切都是正确的,除了一个改变:

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

删除这个:

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

【讨论】:

  • 但我必须重写 OnBind() 方法,否则我将不得不将我的服务声明为抽象
  • 你正在盯着你的服务,所以为什么你需要 onBind() 方法回调。您将获得 onStartCommand() 回调,因为您正在使用 startService() 启动服务
  • 但我们必须重写 onBind() 因为它是 Service 中的抽象方法
  • 您正在从 onBind 返回 null。请返回一个活页夹对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2014-05-27
相关资源
最近更新 更多