Tôi đang cố gắng thực hiện một số báo thức sau khi người dùng chọn thứ gì đó có thời gian từ danh sách và tạo thông báo cho nó tại thời điểm đã cho. Vấn đề của tôi là "showame" mà một putExtra trên Intent của tôi không thể nhận được tại bộ thu phát sóng. Nó luôn nhận giá trị null. Đây là cách tôi làm điều đó cho hầu hết các ý định của tôi nhưng tôi nghĩ rằng thời gian này có lẽ vì pendingIntent hoặc BroadcastReceiver một cái gì đó cần phải được thực hiện differelly. Cảm ơn bạngetExtra từ Intent được khởi chạy từ một pendingIntent
Chức năng sẽ gửi tiếp cận mục đích thông qua mục đích cấp phát
public void setAlarm(String showname,String time) {
String[] hourminute=time.split(":");
String hour = hourminute[0];
String minute = hourminute[1];
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
rightNow.set(Calendar.MINUTE, Integer.parseInt(minute));
rightNow.set(Calendar.SECOND, 0);
long t=rightNow.getTimeInMillis();
long t1=System.currentTimeMillis();
try {
Intent intent = new Intent(this, alarmreceiver.class);
Bundle c = new Bundle();
c.putString("showname", showname);//This is the value I want to pass
intent.putExtras(c);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent);
//Log.e("ALARM", "time of millis: "+System.currentTimeMillis());
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("ALARM", "ERROR IN CODE:"+e.toString());
}
}
Và đây là kết thúc nhận
public class alarmreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
Bundle b = intent.getExtras();
String showname=b.getString("showname");//This is where I suppose to receive it but its null
NotificationManager manger = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"TVGuide Υπενθύμιση", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, tvguide.class), 0);
notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε",
showname, contentIntent);
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
notification.sound = Uri.parse("file:///sdcard/dominating.mp3");
notification.vibrate = new long[]{100, 250, 100, 500};
manger.notify(1, notification);
}
}
Đây là câu trả lời đúng ……… – Necronet
+1, đây là câu trả lời đúng, cần được chấp nhận.Nó thực sự hữu ích, Thx. (BTW, bạn cũng có thể sử dụng một mã yêu cầu khác khi tạo mục đích đang chờ xử lý, với một băm, điều đó khá thuận tiện). – Snicolas