when you start to learn Android, when you finish your homework, you find that the notification you wrote will not appear on the virtual machine on time if you customize an instance, but if you change the time to System.currentTimeMillis (), you will be notified as soon as you click the add button. The desired effect is that the selection dialog box will pop up after clicking the set alarm button. After selecting the time, the add will start timing in the background, and a notification will be generated at the point. I hope there is a big boss to help see what the problem is. Thank you!
this is written in activity:
public void onClickSetAlarm(View arg0){
final String items[] = {"0 min", "5 min", "10 min", "15 min"};
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Choose time")
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
beforeTime = 0;
}
if(which == 1){
beforeTime = 2;
}
if(which == 2){
beforeTime = 10;
}
if(which == 3){
beforeTime = 15;
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar instance = Calendar.getInstance();
instance.set(Calendar.HOUR_OF_DAY, 21);//
instance.set(Calendar.MINUTE, 25);//
instance.set(Calendar.SECOND, 0);//
Intent intent = new Intent(DetailActivity.this,AutoReceiver.class);
intent.setAction("VIDEO_TIMER");
PendingIntent pi = PendingIntent.getBroadcast(DetailActivity.this,0, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5*1000, pi);
}
})
.create();
dialog.show();
}
this is receiver:
package com.example.myschedule;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_FLAG = 0;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("VIDEO_TIMER")){
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,DetailActivity.class), 0);
Notification notify = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("You have class!")
.setContentTitle("You have a class!")
.setContentText("You will have a class soon!")
.setContentIntent(pendingIntent).setNumber(1).build();
notify.flags|= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_FLAG,notify);
}
}
}
this is Manifest.xml:
<receiver
android:name=".AutoReceiver"
android:label="@string/title_activity_detail" >
</receiver>
Thank you!