在用户从列表中选择带有时间的内容并在给定时间为其创建通知后,我尝试发出一些警报.我的问题是我的意图上的putExtra无法在广播接收器上收到的"showame".它总是得到空值.这是我对大部分意图的处理方式,但我想这次可能是因为pendingIntent或者broadcastReceiver需要不同的事情.谢谢
通过挂起的意图发送Intent的函数
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 …Run Code Online (Sandbox Code Playgroud) 当我将额外内容传递给PendingIntent时,sendBroadcastReceiver永远不会收到该消息,因为永远不会调用onReceive()此方法BroadcastReceiver.为什么会这样?
public class MainActivity extends Activity {
private static String SENT = "SMS_SENT";
private static String DELIVERED = "SMS_DELIVERED";
private static int MAX_SMS_MESSAGE_LENGTH = 160;
private static Context mContext;
private BroadcastReceiver sendBroadcastReceiver, deliveryBroadcastReceiver;
private final String DEBUG_TAG = getClass().getSimpleName().toString();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ---when the SMS has been sent---
sendBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
break;
case …Run Code Online (Sandbox Code Playgroud)