6

在我的清單文件中,我已聲明接收者。 (如下)當應用程序未運行時BroadcastReceiver不工作

<receiver android:name=".OnAlarmReceive" /> 

但是,一旦我關閉我的應用程序,我無法獲得警報和通知。顯然,我的Broadcast receiver中的OnReceive電話永遠不會發出。

public class OnAlarmReceive extends BroadcastReceiver 
{ 
@Override 
public void onReceive(Context context, Intent arg1) 
    { 
     //various stuff 
    } 
} 

在MainActivity內部,我的報警管理器類如下所示。

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    Intent intent = new Intent("MY_ALARM_NOTIFICATION"); 
    intent.setClass(this, OnAlarmReceive.class); 
    intent.putExtra("message", message); 
    PendingIntent pendingIntent = PendingIntent 
      .getBroadcast(MainActivity.this, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

    Calendar timeCal = Calendar.getInstance(); 
    timeCal.set(Calendar.HOUR_OF_DAY, hour); 
    timeCal.set(Calendar.MINUTE, minutes); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent); 

,我的表現爲是如下:

<receiver android:name=".OnAlarmReceive"> 
    <intent-filter android:priority="1"> 
     <action android:name="MY_ALARM_NOTIFICATION"/> 
    </intent-filter> 
</receiver> 

我應該怎麼才能收到通知/報警,即使我已經關閉我的應用程序做。後臺服務?

回答

0

你應該增加意圖過濾器清單中,作爲

receiver android:name=".SmsBroadCastReceiver"> 
     <intent-filter android:priority="20"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     </intent-filter> 
    </receiver> 
+1

報警接收是否一樣?因爲你給了短信 – tony9099 2013-05-06 08:58:38

0

由於龔聰說,你需要聲明你的接收機應該聽哪些事件。

例如:

<receiver android:name=".OnAlarmReceive"> 

<intent-filter> 
    <action android:name="MY_ALARM_NOTIFICATION"/> 
</intent-filter> </receiver> 

,然後當你設置報警,使用目的與您的行動:

Intent intent = new Intent("MY_ALARM_NOTIFICATION"); 
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); 
+0

我做到了。 但是,一旦我關閉我的活動,警報不會熄滅。與應用程序運行上面的代碼(即使在我的更改後)工作。但是當我從應用程序管理器中刪除應用程序時,它不會運行。 – tony9099 2013-05-06 09:10:20

+0

你能發表你的部分代碼嗎?你如何設置你的鬧鐘?你使用AlarmManager.RTC_WAKEUP標誌嗎? – 2013-05-06 09:16:53

+0

是的,我這樣做,但是像這樣,我不能把意圖額外的,因爲我不是用意圖調用一個活動,而是在清單中的廣播接收器沒有? – tony9099 2013-05-06 09:27:19

0

您的代碼工作正常!

所有你所要做的就是改變這一行:

alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), 
pendingIntent); 

這一行:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
SystemClock.elapsedRealtime() + 5000, pendingIntent); 

而在「的onReceive」代碼將5000毫秒後運行(5秒),即使應用程序沒有運行

-1

1.Declare接收器在清單文件:

<receiver android:name="your.package.name.TestAlarmReceiver"></receiver> 

請務必記住整個Android系統區分大小寫。所以在AndroidMainfest.xml中檢查拼寫是否正確。

2.如果您爲Receiver創建了PendingIntent,請添加requestCode - 即使它是隨機數!沒有你的onReceive代碼永遠不會被調用!

其啓動AlarmManager看起來應該像下面的功能:

public static void scheduleTestAlarmReceiver(Context context) { 
    Intent receiverIntent = new Intent(context, TestAlarmReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0); 

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender); 
} 

廣播接收器類:

package your.package.name; 

public class TestAlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent arg1) { 
    // your code here! 
    } 
} 

原條款:Why my BroadcastReceiver does not get called?

0

在我的理解,在一些套管取決於實施方式,OS有權調整警報設置時間。因此,請嘗試相應地使用AlarmManager.set(...),AlarmManager.setexact(...)等。在某些情況下,根據製造商(自定義Android操作系統),操作系統可能會阻止火警。

0

爲清單文件中的接收器添加android:exported="true"幫助我即使關閉應用程序(故意由我從應用程序從任務列表中刪除應用程序)接收警報(並因此喚醒應用程序)。

相關問題