Intent.getExtras() always returns null

2019-03-20 21:17发布

I'm trying to run an activity through a notification and event onCreate I would like to "redirect". To this add a thought on information in the Intent class. An important feature is that the class that generates the notification is performed through a service. I retrieve the context from getApplicationContext method provided by the class android.app.Application. Whenever I call method getExtras() is returning null. What am I doing wrong?

public class OXAppUpdateHandler {

    private void addNotification(Context context, int iconID,
           CharSequence tickerText, CharSequence title, CharSequence content) {

        CharSequence notificationTicket = tickerText;
        CharSequence notificationTitle = title;
        CharSequence notificationContent = content;

        long when = System.currentTimeMillis();

        Intent intent = new Intent(context, MainActivity_.class);
        intent.setFlags(
            Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY, 1);

        PendingIntent pendingIntent = 
            PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(
                Context.NOTIFICATION_SERVICE);
        Notification notification = 
            new Notification(iconID, notificationTicket, when);
        notification.setLatestEventInfo(context, notificationTitle, 
                                        notificationContent, pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    public static boolean isUpdateStart(Intent intent) {
        Bundle bundle = intent.getExtras();
        boolean result = bundle != null && 
                         bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY);
        if (result) {
            bundle.remove(OPEN_UPDATE_ACTIVITY_KEY);
        }
        return result;
        }
    }

    @EActivity(R.layout.activity_main)
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (OXAppUpdateHandler.isUpdateStart(getIntent())) {
                startActivity(new Intent(this, UpdateActivity_.class));
            }
        }
    }

2条回答
我命由我不由天
2楼-- · 2019-03-20 21:36

I'm going to lean out the window and guess that your problem is here:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

You are passing intent to getActivity() and expecting that you will get back a PendingIntent that matches your Intent and includes your extras. Unfortunately, if there is already a PendingIntent floating around in the system that matches your Intent (without taking into consideration your Intent extras) then getActivity() will return you that PendingIntent instead.

To see if this is the problem, try this:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

This says that if there is already a PendingIntent that matches your Intent somewhere in the system that it should replace the extras with the ones in your intent parameter.

查看更多
来,给爷笑一个
3楼-- · 2019-03-20 21:37

(1) Check here to make sure you are using the put/get extras correctly as I don't see the code where you are adding data the intent.

(2) It looks like you are not calling get intent and get extra and, as a result, not actually getting anything from the bundle (assuming that information extists). If you are checking for a boolean value you should get the data you place in the bundle as follows:

Bundle bundle = getIntent().getExtras();

if (bundle.getBooleanExtra("WHATEVER"){
   //whatever you want to do in here
} 
查看更多
登录 后发表回答