1

Ich erstelle meine eigene Wecker-App für Android und habe Probleme mit der Benachrichtigung. Ich möchte nap Zeit von der Kennzeichnungspflicht setzen, so dass ich bauen Benachrichtigung mit WirkungAktuelle Uhrzeit abrufen, wenn auf die Benachrichtigungsaktion geklickt wurde

public class MainActivity extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    addNotification(); 
} 

    private void addNotification() { 
     SimpleDateFormat sdfHour = new SimpleDateFormat("HH"); 
     SimpleDateFormat sdfMinute = new SimpleDateFormat("mm"); 
     int timeHour = Integer.parseInt(sdfHour.format(getInstance().getTime())); 
     int timeMinute = Integer.parseInt(sdfMinute.format(getInstance().getTime())); 

     Intent a15 = new Intent(AlarmClock.ACTION_SET_ALARM); 
     a15.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap"); 
     a15.putExtra(AlarmClock.EXTRA_HOUR, timeHour); 
     a15.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+15); 
     a15.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 
     PendingIntent nap15 = PendingIntent.getActivity(this,1,a15,PendingIntent.FLAG_UPDATE_CURRENT); 

     Intent a30 = new Intent(AlarmClock.ACTION_SET_ALARM); 
     a30.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap"); 
     a30.putExtra(AlarmClock.EXTRA_HOUR, timeHour); 
     a30.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+30); 
     a30.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 
     PendingIntent nap30 = PendingIntent.getActivity(this,2,a30,PendingIntent.FLAG_UPDATE_CURRENT); 

     Intent a45 = new Intent(AlarmClock.ACTION_SET_ALARM); 
     a45.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap"); 
     a45.putExtra(AlarmClock.EXTRA_HOUR, timeHour); 
     a45.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute + 3); 
     a45.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 
     PendingIntent nap45 = PendingIntent.getActivity(this,3,a45,PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_alarm_white_24dp) 
       .setContentTitle("Take a nap.") 
       .setContentText("Select nap time.") 
       .addAction(R.drawable.ic_alarm_add_white_24dp, "15min",nap15) 
       .addAction(R.drawable.ic_alarm_add_white_24dp, "30min",nap30) 
       .addAction(R.drawable.ic_alarm_add_white_24dp, "45min",nap45); 


     // Add as notification 
     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(0, mBuilder.build()); 

    } 

aber wenn ich nap Zeit für 15min Wecker eingerichtet für 15 Minuten ab und wählen Sie, wenn ich erstellt Benachrichtigung anstelle der aktuellen Systemzeit. Zum Beispiel, wenn ich app um 5 Uhr abends, und nach 10min (5:10 pm) wähle ich Aktion von Benachrichtigung ist es Alarm für 5 Uhr + 15 nicht für 5:10 Uhr + 15. So ist es möglich, aktuelle Zeit zu erhalten, wenn ich geklickt habe Aktion in der Benachrichtigung? Was muss ich verwenden?

Antwort

0

Sie können die aktuelle Uhrzeit in Stunden und Minuten von Simple erhalten:

SimpleDateFormat sdfHour = new SimpleDateFormat("HH"); 
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm"); 
int timeHour = Integer.parseInt(sdfHour.format(getInstance().getTime())); 
int timeMinute = Integer.parseInt(sdfMinute.format(getInstance().getTime())); 

so wäre es:

Intent a15 = new Intent(AlarmClock.ACTION_SET_ALARM); 
     a15.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap"); 
     a15.putExtra(AlarmClock.EXTRA_HOUR, timeHour); 
     a15.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute + 15); 
     a15.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 
     PendingIntent nap15 = PendingIntent.getActivity(this,1,a15,PendingIntent.FLAG_UPDATE_CURRENT); 
+0

für mich nicht funktioniert. Erhalten Sie immer noch aktuelle Zeit vom ersten App-Lauf, und wenn ich Aktion auf Benachrichtigungszeit geklickt hat, aktualisiert nicht auf aktuellen Wert – Mich

+0

stellen Sie sicher, dass Sie die Int timeHour und int timeMinute zum Zeitpunkt des Klicks auf die Aktionsschaltfläche instanziieren, anstatt wenn die Aktivität gestartet wird – buradd

+0

Jetzt instanziiere ich beide Werte, wenn ich eine Benachrichtigung erstelle, wie ich den Wert bei addAction (int, CharSequence, PendingIntent) in die Benachrichtigung einfügen kann. Wo muss ich initiatiang Werte richtig machen? – Mich

Verwandte Themen