2016-01-29 77 views
2

我有一個警報管理器在某個日期和時間顯示通知。 我安排通知如下:通知的警報管理器不起作用

private void scheduleNotification(String title, String text, String subtext, long futureInMillis) { 

    Intent notificationIntent = new Intent(getActivity(), NotificationPublisher.class); 
    notificationIntent.putExtra("title", title); 
    notificationIntent.putExtra("text", text); 
    notificationIntent.putExtra("subtext", text); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
} 

的廣播接收器:

public class NotificationPublisher extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
     Log.v("receiver", "notification received"); 
     String title = intent.getStringExtra("title"); 
     String text = intent.getStringExtra("text"); 
     String subtext = intent.getStringExtra("subtext"); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     builder.setSmallIcon(R.drawable.checkbox_on_24dp); 
     builder.setContentTitle(title); 
     builder.setContentText(text); 
     builder.setSubText(subtext); 
     builder.setSound(uri); 
     builder.setVibrate(new long[]{1000}); 

     Notification not = builder.build(); 

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


     notificationManager.notify(0, not); 

    } 
} 

清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.christophbielen.simplytodo"> 
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <provider 
     android:name=".data.DataProvider" 
     android:authorities="com.christophbielen.simplytodo" /> 

    <activity 
     android:name=".DetailActivity" 
     android:label="@string/title_activity_detail" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.christophbielen.simplytodo.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".ListCategoriesActivity" 
     android:label="@string/title_activity_list_categories" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.christophbielen.simplytodo.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".AddListActivity" 
     android:label="@string/title_activity_add_list" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.christophbielen.simplytodo.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".EditListActivity" 
     android:label="@string/title_activity_edit_list" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.christophbielen.simplytodo.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".AddItemActivity" 
     android:label="@string/title_activity_add_item" 

     android:theme="@style/AppTheme.NoActionBar"> 
     <!--<meta-data--> 
      <!--android:name="android.support.PARENT_ACTIVITY"--> 
      <!--android:value="com.christophbielen.simplytodo.MainActivity" />--> 
    </activity> 
    <activity 
     android:name=".EditItemActivity" 
     android:label="@string/title_activity_edit_item" 

     android:theme="@style/AppTheme.NoActionBar"> 

    </activity> 
    <receiver android:name=".NotificationPublisher" /> 
</application> 

我打電話安排通知如下:

int year = Integer.parseInt(date.substring(0,4)); 
         int month = Integer.parseInt(date.substring(4,6))-1; 
         int day = Integer.parseInt(date.substring(6,8)); 
         int hour = Integer.parseInt(time.substring(0,2)); 
         int minute = Integer.parseInt(time.substring(2)); 

         Calendar cal=Calendar.getInstance(); 
         cal.set(Calendar.MONTH,month); 
         cal.set(Calendar.YEAR,year); 
         cal.set(Calendar.DAY_OF_MONTH,day); 
         cal.set(Calendar.HOUR_OF_DAY,hour); 
         cal.set(Calendar.MINUTE,minute); 
         cal.set(Calendar.SECOND,0); 

         String subcontent = catName + " due on: " + year + "/" + month+1 + "/" + day + " " + hour + ":" + minute; 

         scheduleNotification("To do!", shoppingListName, subcontent, cal.getTimeInMillis()); 

當我安排通知它似乎工作,但接收器從來沒有收到任何東西。

我到處尋找答案,我無法弄清楚爲什麼警報管理器不起作用。

感謝

回答

0

請使用 -

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (System.currentTimeMillis() + futureInMillis, pendingIntent); 

代替 -

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
0

當你安排與ELAPSED_REALTIME_WAKEUP警報,必須使用SystemClock.elapsedRealtime()爲基準的時間偏移。

AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + futureInMillis, pendingIntent); 

DroidFiddle例如:https://droidfiddle.net/vwkttsz/1