Android通知与AlarmManager,广播和服务和服务、通知、Android、AlarmManager

由网友(闺蜜一声一辈子)分享简介:这是我的code为家政一个通知: myActivity.java 公共类myActivity延伸活动{    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView...

这是我的code为家政一个通知:

myActivity.java

 公共类myActivity延伸活动{    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.mylayout);        CAL = Calendar.getInstance();        //它被设置至10:30        cal.set(Calendar.HOUR,10);        cal.set(Calendar.MINUTE,30);        cal.set(Calendar.SECOND,0);        长启动= cal.getTimeInMillis();        如果(cal.before(Calendar.getInstance())){                 启动+ = AlarmManager.INTERVAL_FIFTEEN_MINUTES;        }        意图mainIntent =新意图(这一点,myReceiver.class);        pIntent = PendingIntent.getBroadcast(这一点,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT);        AlarmManager myAlarm =(AlarmManager)getSystemService(ALARM_SERVICE);        myAlarm.setRepeating(AlarmManager.RTC_WAKEUP,启动,AlarmManager.INTERVAL_FIFTEEN_MINUTES,pIntent);    }} 

myReceiver.java

 公共类myReceiver扩展广播接收器{    @覆盖    公共无效的onReceive(上下文C,意图我){       意图myService1 =新意图(C,myAlarmService.class);       c.startService(myService1);    }} 
Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏

myAlarmService.java

 公共类myAlarmService延伸服务{@覆盖公众的IBinder onBind(意向为arg0){    返回null;}@覆盖公共无效的onCreate(){   super.onCreate();}@燮pressWarnings(德precation)@覆盖公共无效调用onStart(意向意图,诠释startId){    super.onStart(意向,startId);    displayNotification(); }@覆盖公共无效的onDestroy(){    super.onDestroy();}公共无效displayNotification(){     意图mainIntent =新意图(这一点,myActivity.class);     的PendingIntent pIntent = PendingIntent.getActivity(这一点,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT);     NotificationManager纳米=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);     Notification.Builder建设者=新Notification.Builder(本);     builder.setContentIntent(pIntent)     .setAutoCancel(真)     .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());}} 

的Andr​​oidManifest.xml

 <使用许可权的android:NAME =android.permission.WAKE_LOCK/>.........<服务机器人:名字=机器人myAlarmService:启用=真/><接收机器人:myReceiverNAME = /> 

如果时间还没有过去没有一切完美。通知出现时它必须出现。

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

我无法弄清楚什么是错的吧。你能不能帮我请(并解释原因,如果你找到一个解决方案),在此先感谢:)

解决方案

  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 .

阅读全文

相关推荐

最新文章