android - 服务在 Activity 的 'death' 处崩溃

标签 android service crash

我有一个启动服务的 Activity 。

在我的 Activity 中:

startService(new Intent(this, MyService.class));

在我的服务中,onStart():

/* Show notification */
int icon = R.drawable.icon;
tickerText = getResources().getString(R.string.app_name);
long when = System.currentTimeMillis();
contentTitle = getResources().getString(R.string.app_name);
contentText = getResources().getString(R.string.running);

Intent notificationIntent = new Intent(this, activityClass).setAction(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

notification.flags = Notification.FLAG_ONGOING_EVENT;

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);

startForeground(0, notification);

该服务有一个计时器,每 3-4 分钟执行一次工作,并在可能的情况下向 Activity 广播信息。当 Activity 关闭时,Service 应该继续工作。

当我运行该应用程序并返回主屏幕时,服务会继续运行。但是过了一会儿,我收到了这些 logcat 消息,服务停止了:

07-03 16:55:09.440: INFO/ActivityManager(583): Process com.myapp (pid 11665) has died. 07-03 16:55:09.440: WARN/ActivityManager(583): Scheduling restart of crashed service com.myapp/.MyService in 5000ms

我怎样才能避免这种情况?

最佳答案

所提供的答案对我没有帮助。但是我找到了一个非常简单的方法来做到这一点,在 list 中添加

    <service android:name="..." android:process=":remote" />

http://developer.android.com/guide/topics/manifest/service-element.html

目前只是测试,我的 Activity 已经被杀死,但我的服务正在运行。

关于android - 服务在 Activity 的 'death' 处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6563641/

相关文章:

wcf - 远程服务器返回意外响应 : (400) Bad Request

iOS - 多通知扩展服务?

linux - Spring Boot 应用程序即服务

windows - 尝试使用Progressbar运行tkinter时Python崩溃

android - Kotlin:EditText 在 afterTextChanged() 中为空

android - 为什么这个 future 永远无法完成?

java - 数据库到android,用php

android - 如何在带有 TabLayout 的 Fragment 中使用 CardView 行实现 RecyclerView

c++ - 为什么我的所有 C++ 程序都以 0xc0000139 退出?

启动应用程序时 Ruby 崩溃。是环境吗?