Intent.getExtras()始终返回nullIntent、getExtras、null

2023-09-05 07:33:14 作者:微笑要带着眼泪才耐看づ

我试图运行通过通知和事件的onCreate 我想重定向的活动。为此,在意图类的信息添加一个念头。一个重要的特点是,生成该通知的类是通过服务执行。我检索 getApplicationContext 的背景下由类提供的方法 android.app.Application 。每当我调用方法 getExtras()将返回。我究竟做错了什么?

 公共类OXAppUpdateHandler {

    私人无效addNotification(上下文的背景下,诠释iconID,
           CharSequence的tickerText,CharSequence的标题,内容的CharSequence){

        CharSequence的notificationTicket = tickerText;
        CharSequence的notificationTitle =称号;
        CharSequence的notificationContent =内容;

        时长= System.currentTimeMillis的();

        意向意图=新的意图(背景下,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(上下文,0,意图,0);

        NotificationManager notificationManager =
            (NotificationManager)context.getSystemService(
                Context.NOTIFICATION_SERVICE);
        通知通知=
            新的通知(iconID,notificationTicket时);
        notification.setLatestEventInfo(背景下,notificationTitle,
                                        notificationContent,pendingIntent);
        notificationManager.notify(NOTIFICATION_ID,通知);
    }

    公共静态布尔isUpdateStart(意向意图){
        捆绑包= intent.getExtras();
        布尔结果=捆绑= NULL和放大器;!&安培;
                         bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY);
        如果(结果){
            bundle.remove(OPEN_UPDATE_ACTIVITY_KEY);
        }
        返回结果;
        }
    }

    @EActivity(R.layout.activity_main)
    公共类MainActivity延伸活动{
        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            如果(OXAppUpdateHandler.isUpdateStart(getIntent())){
                startActivity(新意图(这一点,UpdateActivity_.class));
            }
        }
    }
 

解决方案

我要瘦窗外,猜测你的问题就在这里:

  PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意向,0);
 

正在通过意图 getActivity()并期望你会得到一个 PendingIntent 相匹配的意图,包括你的额外的。不幸的是,如果已经有一个 PendingIntent 的匹配系统,浮在你的意图(没有考虑到你的意图演员),那么 getActivity()将返回你的 PendingIntent 代替。

AndroidIntent总结

要查看是否有问题,试试这个:

  PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意图,
                    PendingIntent.FLAG_UPDATE_CURRENT);
 

这表示,如果已经有一个 PendingIntent 相匹配的意图某处系统,就应更换与那些在你的意图演员参数。

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

解决方案

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.

 
精彩推荐
图片推荐