android报警在onReceive期间死亡

时间:2012-03-31 17:52:49

标签: android android-alarms

我有一个计时器应用程序,用系统警报(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 杀了我的应用程序以及如何防止这种情况?它只发生一次......

1 个答案:

答案 0 :(得分:0)

BroadcastReceiver不适用于长时间运行的操作。 BroadcastReceiver被认为是非常短暂的。我已经从其他人那里了解到BroadcastReceiver s应该持续最多50毫秒,如果它不再存在,你应该开始Service

因此,您应该设置闹钟以启动Service,或者从Service中启动BroadcastReceiver并移动任何耗时的代码(例如Handler.postDelayed() )到Service