与所有的意图,但第一个接到群众演员不正确 [英] Incorrect extras received with all intents but the first one

查看:93
本文介绍了与所有的意图,但第一个接到群众演员不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些可以用来为未来事件的提醒一个小应用程序。该应用使用了AlarmManager设置,当用户需要提醒的时间。当警报响起时,一个BroadcastReceiver注册这反过来启动的服务通过敬酒,并在状态栏的通知,以通知用户。

I have a small application which can be used to set reminders for future events. The app uses an AlarmManager to set the time for when the user should be reminded. When the alarm goes off, a BroadcastReceiver registers this and in turn starts a service to notify the user via a toast and a notification in the status bar.

为了显示在通知和烤面包正确的信息,一些额外的信息与意图传承下去。提醒注册的第一次,由广播接收器接收并传递给服务的信息是正确的。但对于每个以下提示(即,由广播接收器接收到的每一个新的意图),该信息保持不变,即使发送的信息是不同的。

In order to display the correct information in the notification and toast, some extra information is passed along with the intent. The first time a reminder is registered, the info received by the BroadcastReceiver and passed on to the service is correct. But for each following reminder (i.e. each new intent received by the BroadcastReceiver) this information stays the same even when the information sent is different.

作为一个例子,如果该字符串foo被置于作为一个额外的与第一意图,foo被正确地由广播接收器提取。如果栏在第二意图被置于作为一个额外的,foo的仍然是由广播接收器提取。

As an example, if the string "foo" is put as an extra with the first intent, "foo" is correctly extracted by the BroadcastReceiver. If "bar" is put as an extra in the second intent, "foo" is still extracted by the BroadcastReceiver.

这是code,它注册报警,并传递意图(主UI类):

This is the code that registers the alarm and passes the intent (main ui-class):

Intent intent = new Intent(ACTION_SET_ALARM);
intent.putExtra("desc", desc);
intent.putExtra("time", time);
intent.putExtra("dbId", dbId);
intent.putExtra("millis", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(quickAlert.this, 0, intent, 0);

// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, millis, pIntent);

本的onReceive() - 方法的广播接收器类:

The onReceive()-method in the BroadcastReceiver class:

@Override
public void onReceive(Context context, Intent intent) {

	Intent i = new Intent(context, AlertService.class);

	String desc = intent.getStringExtra("desc").equals("") ? "": ": " + intent.getStringExtra("desc");
	String time = intent.getStringExtra("time");
	long dbId = intent.getLongExtra("dbId", -1);
	long millis = intent.getLongExtra("millis", -1);

	i.putExtra("desc", desc);
	i.putExtra("time", time);
	i.putExtra("dbId", dbId);
	i.putExtra("millis", millis);
	Log.d(TAG, "AlertReceiver: " + desc + ", " + time + ", " + dbId + ", " + millis);

	Toast.makeText(context, "Reminder: " + desc, Toast.LENGTH_LONG).show();
	context.startService(i);
}

意向过滤器清单:

The intent-filter in the manifest:

<receiver android:name=".AlertReceiver">
        <intent-filter>
        	<action android:name="com.aspartame.quickAlert.ACTION_SET_ALARM" />
        </intent-filter>
    </receiver>

我已经坚持了一段时间了,所以帮助是非常AP preciated!

I've been stuck with this for some time now, so help is very much appreciated!

推荐答案

尝试添加<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#FLAG%5FUPDATE%5FCURRENT\"><$c$c>FLAG_UPDATE_CURRENT你的的PendingIntent 如果您通过 getBroadcast创建它()

这篇关于与所有的意图,但第一个接到群众演员不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆