0

これは、単一の通知を管理するための私のコードです:

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 分と仮定しましょう) 通知は毎回発生します... アプリを閉じて再度開くと、通知をクリックすると... 本当に奇妙な動作をします。

何が悪いのかわかりません。助けてください(解決策が見つかった場合は理由を説明してください)、事前に感謝します:)

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 に答える