使用通知和警报

时间:2014-03-24 11:16:11

标签: android android-notifications android-alarms

我在我的Android应用程序中使用通知和警报但我不明白为什么即使我设置警报,每次启动应用程序时都会看到通知。 这是MainActivity:

public void setRepeatingAlarm() {
         Intent intent = new Intent(this, MyAlarmService.class);


         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
         intent, PendingIntent.FLAG_CANCEL_CURRENT);

         long startAt;
         long period;

         SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);

         long dif = System.currentTimeMillis() - mPref.getLong("UPDATE_TIME", 0);

         if (dif >= UPDATE_PERIOD) {
           startAt = 0;
           period = UPDATE_PERIOD;
         } else {
           startAt = dif;
           period = dif;
         }


         am.setRepeating(AlarmManager.RTC_WAKEUP, startAt, period,pendingIntent);
}

这是MyAlarmService:

public class MyAlarmService extends BroadcastReceiver {

 NotificationManager nm;

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


  nm = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  CharSequence from = "Locali Torino";
  CharSequence message = "Visita le serate!";
  Intent action = new Intent(context, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    action, 0);



  Notification notif = new Notification(R.drawable.disco,
    "Visita le serate!", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  notif.flags |= Notification.FLAG_AUTO_CANCEL;
  nm.notify(0, notif);  
  SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
    SharedPreferences.Editor mEditor = mPref.edit();



    long time = System.currentTimeMillis();
    mEditor.putLong("UPDATE_TIME", time);
    mEditor.commit();
 }
}

我希望每天都能同时看到通知,而不是每次都打开应用。谢谢。

修改

这是OnCreate:

 @Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   StrictMode.enableDefaults(); //STRICT MODE ENABLED

   context=MainActivity.this;


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





   locale = (TextView) findViewById(R.id.locale7);
   lunedi = (TextView) findViewById(R.id.lunedi7);
   martedi = (TextView) findViewById(R.id.martedi7);
   mercoledi = (TextView) findViewById(R.id.mercoledi7);
   giovedi = (TextView) findViewById(R.id.giovedi7);
   venerdi = (TextView) findViewById(R.id.venerdi7);
   sabato = (TextView) findViewById(R.id.sabato7);
   domenica = (TextView) findViewById(R.id.domenica7);

   Typeface font = Typeface.createFromAsset(getAssets(), "Chunkfive.otf");
   locale.setTypeface(font);
   lunedi.setTypeface(font);
   martedi.setTypeface(font);
   mercoledi.setTypeface(font);
   giovedi.setTypeface(font);
   venerdi.setTypeface(font);
   sabato.setTypeface(font);
   domenica.setTypeface(font);

    getData();



}

1 个答案:

答案 0 :(得分:0)

每次调用Activity时首先调用UPDATE_PERIOD将为零。因此,startUp始终为零。

秒 - 每次启动应用程序时,都会从Activity的oncreate调用setRepeatingAlarm()。所以你的闹钟正在休息。

第三步 - 将当前时间添加到setrepeating(),否则将在设置时调用闹钟。

所以做出其他改变(根据你的要求)和这个改变。

 am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+startAt, period,pendingIntent);

更新:

将UPDATE_PERIOD期间存储在sharedpreference中并在执行oncreate之前检索它