Android:无法从意图设置/检索数据
问题描述:
我想在我的应用程序中安排本地通知。这是我的RootReceiver
班。Android:无法从意图设置/检索数据
public class RebootReceiver extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(Context context, Intent intent) {
Debug.waitForDebugger();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, AlarmScheduler.class);
PendingIntent intentExecuted = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
if (!GeneralMethods.getBooleanPreference(context, ProperatiPreferences.APP_FIRST_LAUNCH)) {
intent1.putExtra(EVENT_CATEGORY, "");
now.add(Calendar.HOUR, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), intentExecuted);
} else if (!GeneralMethods.getBooleanPreference(context, ProperatiPreferences.SEARCH_AFTER_THREE_DAYS)) {
intent1.putExtra(EVENT_CATEGORY, "");
now.add(Calendar.DATE, 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), intentExecuted);
}
}
}
在这里你可以看到,我想创造一个我希望把一些数据(intent1
)的意图。然而,意图总是空的,没有任何额外的内容。我究竟做错了什么?
下面是我如何尝试从意图检索额外的东西。
public class AlarmScheduler extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("com.properati.user", "AlarmScheduler.onReceive() called");
Intent eventService = new Intent(context, AlarmService.class);
context.startService(eventService);
}
最后我AlarmService
类:
public class AlarmService extends Service {
private String EVENT_CATEGORY = "notification_event";
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
Log.d("com.properati.user", "event received in service: " + new Date().toString());
if(intent.getStringExtra(EVENT_CATEGORY).equals(ProperatiPreferences.APP_FIRST_LAUNCH)){
new PushNotification(getApplicationContext()).scheduleNonOpenedNotification(getApplicationContext());
}else if(intent.getStringExtra(EVENT_CATEGORY).equals(ProperatiPreferences.SEARCH_AFTER_THREE_DAYS)){
new PushNotification(getApplicationContext()).scheduleNoSearchAfterThreeDays(getApplicationContext());
}
return Service.START_NOT_STICKY;
}
答
试试下面的代码AlarmScheduler类
public class AlarmScheduler extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("com.properati.user", "AlarmScheduler.onReceive() called");
Intent eventService = new Intent(context, AlarmService.class);
eventService.putExtra(intent.getStringExtra(EVENT_CATEGORY, ""));
context.startService(eventService);
}
答
后,我检查了Android框架中的PendingIntent源,意图参数将被new Intent(intent)
克隆。因此在将它传递给PendingIntent构造函数之前,您需要将所有数据设置为intent1。
@Override
public IIntentSender getIntentSender(int type,
String packageName, IBinder token, String resultWho,
int requestCode, Intent[] intents, String[] resolvedTypes,
int flags, Bundle options, int userId) {
enforceNotIsolatedCaller("getIntentSender");
// Refuse possible leaked file descriptors
if (intents != null) {
if (intents.length < 1) {
throw new IllegalArgumentException("Intents array length must be >= 1");
}
for (int i=0; i<intents.length; i++) {
Intent intent = intents[i];
if (intent != null) {
if (intent.hasFileDescriptors()) {
throw new IllegalArgumentException("File descriptors passed in Intent");
}
if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
(intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
throw new IllegalArgumentException(
"Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
}
intents[i] = new Intent(intent);
}
}
if (resolvedTypes != null && resolvedTypes.length != intents.length) {
throw new IllegalArgumentException(
"Intent array length does not match resolvedTypes length");
}
}
+0
即便如此没有成功报警服务接收到的意图仍是空 – user5035668
是我的朋友,这是正确的答案。我错过了演员阵容没有发送到第三项活动!谢谢! – user5035668