且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android通知与AlarmManager,广播和服务

更新时间:2023-02-26 21:52:16

  INT TEMP = calTemp.getTime()的compareTo(calendar.getTime());
    如果(温度大于0){    }其他{
        alarmManager.set(AlarmManager.RTC,calendar.getTimeInMillis(),
                pendingIntent1);    }

在这里 calTemp 给当前的时间和日历给我想火报警的时间。因此,根据上述code。如果时间已过去那么的通知不会触发肯定的。

this is my code for menage a single notification:

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"/>  

IF the time has NOT past yet everything works perfectly. The notification appears when it must appear.

BUT if the time HAS past (let's assume it is 10.31 AM) the notification fires every time... when I close and re-open the app, when I click on the notification... it has a really strange behavior.

I can't figure out what's wrong in it. Can you help me please (and explain why, if you find a solution), thanks in advance :)

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

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

    }

here calTemp gives current time and calender gives the time i want to fire the alarm. So according to above code if the time has already past then the notification will not fire for sure .