2017-05-01 25 views
0

我正在使用Alarm Manager來調用接收方法的通知。我在裏面放了一些調試代碼,發現這個過程甚至都沒有進入。無法進入BroadcastReceive的onReceive方法

這裏是我的代碼:

的Manifest.xml

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".Menu"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".ContentInput" 
     /> 

    <receiver 
     android:process=":remote" 
     android:name=".NoticationBroadcast"/> 
</application> 

Menu.java(主類)

Button noti = (Button) findViewById(R.id.noti); 
noti.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.e(ErrorTag, "before onclick"); 
     scheduleNotification(getNotification("ggggg"),2000); 
     Log.e(ErrorTag, "after onclick"); 
    } 
}); 

private Notification getNotification(String content) { 
    Notification.Builder builder = new Notification.Builder(this); 
    builder.setContentTitle("Scheduled Notification"); 
    builder.setContentText(content); 
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert); 

    Log.e(ErrorTag, "finish get notificattion"); 

    return builder.build(); 
} 

private void scheduleNotification(Notification notification, int delay) { 

    Intent notificationIntent = new Intent(this, NoticationBroadcast.class); 
    notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION_ID, 1); 
    notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION, notification); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Log.e(ErrorTag, "after getBroadcast"); 

    long futureInMillis = SystemClock.elapsedRealtime() + delay; 
    Calendar calendar = Calendar.getInstance(); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
    Log.e(ErrorTag, "after alarm"); 

} 

Receiver類

public class NoticationBroadcast extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification-id"; 
    public static String NOTIFICATION = "notification"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.e("ERROR", "beginning of on receive"); 

     NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
     notificationManager.notify(id, notification); 
     Log.e("ERROR", "end of on receive"); 
    } 
} 

編輯:

我改了一行代碼

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
      + (5000), pendingIntent); 

它可以正常使用。但我不知道爲什麼。如果有人知道發生了什麼,請告訴我。謝謝!

+0

我覺得你的方法如果我錯了,請糾正我的錯誤。您正在向廣播接收器發送通知對象,可能無法正常工作。請嘗試使用服務進行通知。 –

+0

你可以通過谷歌的通知獲取很多簡單的鬧鐘管理器教程。 –

+0

我想我解決了它。但我不知道我做了什麼:O – richardrun

回答

0

在這裏看到:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

您調度作業只有立即執行,這顯然不會被解僱。

而在第二版(工作版本),你比現在5000毫秒後調度作業:

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
       + (5000), pendingIntent); 

更改爲這會按預期工作:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 5000, 
         pendingIntent);