【发布时间】:2012-04-01 03:19:32
【问题描述】:
我有一个计时器应用程序,它通过系统警报 (RTC_WAKEUP) 唤醒设备并打开我的应用程序。上千次成功报警后,只是发生了,设置的报警并没有完全成功。它在 onReceive() 期间死亡并且没有启动我的应用程序,也没有触发系统通知。这是 BroadcastReceiver 的 onReceive() 方法:
public void onReceive(Context context, Intent intent) {
Log.i("timer", "timer's end broadcast received at: " + (System.currentTimeMillis() / 1000) );
m_Context = context;
Bundle extras = intent.getExtras();
final int id = extras.getInt("timer_id");
Intent activityIntent = new Intent(m_Context, TinyTimerActivity.class);
activityIntent.putExtra("timer_id", id);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
m_Context.startActivity(activityIntent);
m_SharedPrefs = PreferenceManager.getDefaultSharedPreferences(m_Context);
// start the alarm sound
final AudioManager localAudioManager = (AudioManager)m_Context.getSystemService("audio");
final int ringerMode = localAudioManager.getRingerMode();
final boolean audibleInSilentMode = m_SharedPrefs.getBoolean("audible_in_silent_mode", true);
final boolean silentAlarm = m_SharedPrefs.getBoolean("silent_alarm", false);
// and now load the alarm sound and play it for the desired time
showFinishedNotification(!silentAlarm && (ringerMode != AudioManager.RINGER_MODE_SILENT || audibleInSilentMode));
// cancel the alarm after some time
final int duration = Integer.parseInt(m_SharedPrefs.getString("alarm_length", "-1"));
if (duration > 0 ) {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
((NotificationManager)m_Context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(NOTIFICATION_TIMER_FINISED_ID);
}
}, duration * 1000);
}
}
当警报被触发时,我正在使用 gReader 应用程序。这是 logcat(我的应用是 sk.martinflorek.TinyTimer):
I( 146) Start proc sk.martinflorek.TinyTimer for broadcast sk.martinflorek.TinyTimer/.timers.TimerReceiver: pid=18307 uid=10070 gids={3003} (ActivityManager)
I(18307) Pub sk.martinflorek.TinyTimer.providers.TimersProvider: sk.martinflorek.TinyTimer.providers.TimersProvider (ActivityThread)
I(18307) timer's end broadcast received at: 1333208420 (timer)
I( 146) Starting: Intent { flg=0x30000000 cmp=sk.martinflorek.TinyTimer/.TinyTimerActivity (has extras) } from pid 18307 (ActivityManager)
D(18198) couldn't save which view has focus because the focused view com.noinnion.android.greader.reader.ui.view.ItemWebView@406dd4f0 has no id. (PhoneWindow)
I( 146) No longer want android.process.media (pid 17918): hidden #16 (ActivityManager)
I( 146) Sending signal. PID: 18307 SIG: 9 (Process)
I( 146) Kill sk.martinflorek.TinyTimer (pid 18307): provider com.android.providers.media.MediaProvider in dying process android.process.media (ActivityManager)
I( 146) Process sk.martinflorek.TinyTimer (pid 18307) has died. (ActivityManager)
为什么 android.process.media 杀死了我的应用程序以及如何防止这种情况发生?它只发生过一次......
【问题讨论】:
-
值得注意的是,如果您的应用为内容提供者持有一个打开的 Cursor 对象,就会发生这种情况。由于 MediaProvider 涉及您的应用程序崩溃,因此 cursor.close() 可能没有在某处被调用。
-
@acj BroadcastReceiver 没有打开任何游标(应用程序本身在启动时会打开一些游标),据我所知,应用程序可以同时打开多个游标。这仍然是一个问题吗?
-
有可能。在我正在考虑的场景中,您使用该应用程序一段时间,从而打开一些光标,然后导航到其他应用程序。您的应用程序已停止但仍保留在内存中。在某些时候(也许当您的 BroadcastReceiver 碰巧正在运行时),MediaProvider 碰巧被杀死了。由于您仍然打开光标,因此您的应用程序也会被终止。关闭游标应该防止这个问题。
-
@acj 在应用中,没有为MediaProvider打开游标,所以它们不应该干扰和崩溃应用,或者?
-
很难说。您与 MediaProvider 的交互可能是间接的。如果应用程序的活动没有释放 onStop 中的所有内容,则可能是泄漏了将其绑定到 MediaProvider 的资源或其他在链中与 MediaProvider 交互的资源。我希望我们有更好的调试工具来解决这类问题。