Android:无法设置/检索意图数据

时间:2015-08-03 11:06:52

标签: java android android-intent

我正在尝试在我的应用中安排本地通知。这是我的RootReceiver课程。

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;
}

2 个答案:

答案 0 :(得分:1)

在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);
}

答案 1 :(得分:0)

在我检查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");
        }
    }