0

这是我管理单个通知的代码:

我的活动.java

public class myActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);


        cal = Calendar.getInstance();
        // it is set to 10.30
        cal.set(Calendar.HOUR, 10);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);

        long start = cal.getTimeInMillis();
        if(cal.before(Calendar.getInstance())) {
                 start +=  AlarmManager.INTERVAL_FIFTEEN_MINUTES;
        }

        Intent mainIntent = new Intent(this, myReceiver.class);
        pIntent = PendingIntent.getBroadcast(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager myAlarm = (AlarmManager)getSystemService(ALARM_SERVICE);
        myAlarm.setRepeating(AlarmManager.RTC_WAKEUP, start,  AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);
    }
}

myReceiver.java

public class myReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context c, Intent i) {
       Intent myService1 = new Intent(c, myAlarmService.class);
       c.startService(myService1);
    }   
}

myAlarmService.java

public class myAlarmService extends Service {

@Override
public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public void onCreate()  {

   super.onCreate();
}

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    displayNotification();
 }    

@Override
public void onDestroy()  {

    super.onDestroy();
}


public void displayNotification() {

     Intent mainIntent = new Intent(this, myActivity.class);
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);      

     NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
     Notification.Builder builder = new Notification.Builder(this);

     builder.setContentIntent(pIntent)
     .setAutoCancel(true)
     .setSmallIcon(R.drawable.ic_noti)
     .setTicker(getString(R.string.notifmsg))        
     .setContentTitle(getString(R.string.app_name))
     .setContentText(getString(R.string.notifmsg));

     nm.notify(0, builder.build());
}    

}

AndroidManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
...
...
...
<service android:name=".myAlarmService" android:enabled="true" />
<receiver android:name=".myReceiver"/>  

如果时间还没有过去,一切都完美无缺。通知在必须出现时出现。

但是如果时间已经过去(假设它是上午 10.31),通知每次都会触发......当我关闭并重新打开应用程序时,当我点击通知时......它有一个非常奇怪的行为。

我无法弄清楚它有什么问题。你能帮我吗(如果你找到解决方案,请解释原因),在此先感谢:)

4

3 回答 3

0
int temp = calTemp.getTime().compareTo(calendar.getTime());
    if(temp > 0){

    }else{
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
                pendingIntent1);

    }

这里calTemp给出了当前时间并calender给出了我想要发出警报的时间。所以根据上面的代码,如果时间已经过去,那么notification肯定不会触发。

于 2013-10-31T07:49:12.083 回答
0

将显示通知放在 if 语句中,以便将当前时间与通知设置时间进行比较,如果当前时间早于设置时间则显示通知,否则什么也不做。

于 2013-10-29T12:14:23.227 回答
0

嗨,我遇到了同样的问题,并在这篇 SO 帖子中找到了解决方案,基本上这个想法是依赖 AlarmManager、Receiver 但避免使用 Service。

由于您使用服务只是为了构建和显示通知,您可能会发现我的方法很有用。

让我知道。

于 2016-09-05T09:01:35.130 回答