2014-03-24 61 views
0

我在我的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(); 



} 
+0

后做任何事情UR的OnCreate活动 –

+0

前获取它,我在这里 – ddd

+0

贴吧被定义UPDATE_PERIOD哪里,什么是它的默认值 –

回答

0

第一件事,每次活动被称为UPDATE_PERIOD将是零。所以启动时始终为零。

秒 - 每次启动一个应用程序时,您都会从Activity的oncreate调用setRepeatingAlarm()。所以你的警报正在休息加班。

第三次 - 将当前时间添加到setrepeating(),否则将在设置时调用警报。

因此,进行其他更改(根据您的要求)和这一个。

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

更新:

商店UPDATE_PERIOD期sharedpreference和OnCreate中

+0

我该怎么做才能确保没有看到每次打开应用程序时都会收到通知? – ddd

+0

我应该在哪里保存它并在哪里读取值? – ddd

+0

给我你的完整项目 –