2013-12-13 148 views
0

爲什麼我的通知有時不顯示?有時我的通知沒有顯示

我在MainActivity創建setRecurringAlarm(this);,我想利用這個代碼來顯示通知每30分鐘

這是我的代碼:

private void setRecurringAlarm(Context context) { 
    Log.d("MyService", "setRecurringAlarm() activated..."); 
     Calendar updateTime = Calendar.getInstance(); 
     updateTime.setTimeZone(TimeZone.getDefault()); 
     updateTime.set(Calendar.HOUR_OF_DAY, 12); 
     updateTime.set(Calendar.MINUTE, 30); 
     Intent downloader = new Intent(context, MyStartServiceReceiver.class); 
     downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent); 
    } 

這是我MyStartServiceReceiver:

public class MyStartServiceReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
     Intent dailyUpdater = new Intent(context, MyService.class); 
     context.startService(dailyUpdater); 
     Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive"); 
     } 
    } 

這是我的服務:

public class MyService extends IntentService { 
    public MyService() { 
     super("MyServiceName"); 
    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d("MyService", "Execute MyTask"); 
     new MyTask().execute(); 
     this.sendNotification(this); 
    } 
    private class MyTask extends AsyncTask<String, Void, Boolean> { 
     @Override 
     protected Boolean doInBackground(String... strings) { 
       Log.d("MyService", "Calling doInBackground() in MyTask"); 
       return false; 
     } 
    }   
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    private void sendNotification(Context context) { 
     Log.d("MyService", "Execute sendNotification()"); 
     // here is my notification code --> http://stackoverflow.com/questions/20558619/how-to-set-notification-to-clear-itself-on-click 

    } 
} 

回答