2014-09-19 55 views
2

在我的Android應用程序..我把一個通知..其實我的應用程序是一個命理應用程序..所以我打算每天從應用程序發送一個通知01.00 AM,新的預測可供用戶使用。 ..我可以使用報警管理器發送通知。但我的問題是,每當用戶再次打開此通知時,通知就會反覆出現。但我需要通知在一天內發送一次。一旦用戶打開通知,不需要在同一天發送通知。如果有人可以幫忙,請幫助。我在下面給我的代碼。Android AlarManager通知重複

MainActivity

AlarmManager am; 

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_main); 
     //Context context=MainActivity.this; 


     am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     setRepeatingAlarm(); 


private void setRepeatingAlarm() 
    { 

     // TODO Auto-generated method stub 
     Calendar calendar = Calendar.getInstance(); 



      calendar.set(Calendar.HOUR_OF_DAY, 1); 
      calendar.set(Calendar.MINUTE, 00); 
      calendar.set(Calendar.SECOND, 0); 
      calendar.set(Calendar.AM_PM,Calendar.AM); 

     Intent intent = new Intent(this, AlarmReceiver.class); 
     // PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
     // intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,0); 
     // am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 
     // (1000 * 1000), pendingIntent); 
      am.set(1, System.currentTimeMillis(),pendingIntent); 
      System.out.println("Calling Alaram..."); 

    } 

MyReceiver

public class AlarmReceiver extends BroadcastReceiver 

{ 
    private final String SOMEACTION = "com.jocheved.alarm.ACTION"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     generateNotification(context,"You have new predictions"); 
     String action = intent.getAction(); 
     if (SOMEACTION.equals(action)) { 
      //do what you want here 
      generateNotification(context,"You have new Predictions"); 


     } 
    } 

    @SuppressWarnings("deprecation") 
    private void generateNotification(Context context, String message) { 
      System.out.println(message+"++++++++++2"); 
      int icon = R.drawable.ic_launcher; 
      long when = System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(icon, message, when); 
      String title = context.getString(R.string.app_name); 
     // String subTitle = context.getString(R.string.app_name); 
      String subTitle = "You have new Predictions"; 
      Intent notificationIntent = new Intent(context, DirectCalculation.class); 
      notificationIntent.putExtra("content", message); 
      PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      notification.setLatestEventInfo(context, title, subTitle, intent); 
      //To play the default sound with your notification: 
      notification.defaults |= Notification.DEFAULT_SOUND; 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notificationManager.notify(0, notification); 



    } 

} 

回答

2

的問題是,你設置的報警onCreate(),所以每次活動開始時,它設置爲當前時間發出報警,這立即啓動,啓動活動,再次設置另一個警報。您應該使用AlarmManager#setRepeating()方法將鬧鐘設置爲一次,並且每天重複一次。

alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
    AlarmManager.INTERVAL_DAY, intent); 

你不需要調用setRepeatingAlarm()每次創建活動的時間,所以你需要確定要如何跟蹤它是否被設置。

+0

非常感謝!你是一個救生員。 @Mike你認爲你可以在這裏幫助我http://goo.gl/MAjgTh – eddy 2014-09-20 04:12:43