带有AlarmManager,广播和服务的Android通知

时间:2013-10-28 15:50:08

标签: android service notifications alarmmanager broadcast

这是我的单一通知代码:

myActivity.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 AM),通知每次都会触发...当我关闭并重新打开应用程序时,当我点击通知时......它有一种非常奇怪的行为

我无法弄清楚它有什么问题。你能帮帮我(并解释为什么,如果你找到解决方案),提前谢谢:)

3 个答案:

答案 0 :(得分:0)

将显示通知放在if语句中,以便将当前时间与通知设置时间进行比较,如果当前时间在设置时间之前,则显示通知,否则不执行任何操作。

答案 1 :(得分:0)

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

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

    }

这里calTemp给出了当前时间,calender给出了我想要发出警报的时间。因此,根据上面的代码,如果时间已经过去,那么notification将无法确定。

答案 2 :(得分:0)

您好我有same problem and found a solution in this SO post,基本上我的想法是依赖AlarmManager,Receiver但避免使用服务。

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

让我知道。