【问题标题】:Issue with fetching extras from intent:从意图中获取额外内容的问题:
【发布时间】:2015-09-24 09:19:28
【问题描述】:

我有以下广播接收器:

 mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                System.out.println("onReceive");
                final String token1 = intent.getStringExtra("token");
                System.out.println("onReceive" + token1);
                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    final String token = intent.getStringExtra("token");
                    System.out.println("Token fetched");
                    new PostLoginWithGCM(cleanKey, token).execute();
                } else {
                    System.out.println("Token null");
                }
            }
        };

输出是:

09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceive
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceivefy6gmbUODWk:APA91bHf3PrCQmzZXnfR5LU40C7Mu1jhHenX8SvYR2OvQR6A3npXKa4NF8J-eZtHoxO7QAbTe_S94L8IttuU7ZrT5S97mucJb6EmmF92y3Hi60lrGb6cBfjDHj9fYDfaL8chion-Uyh5
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: Token null

所以第一个 System.out onReceive 工作正常。

第二个 System.out 给了我与 Intent 一起传递的令牌值。

最后我检查了我的附加值是否为空,令我惊讶的是,我的附加值是空的,而我只是在空值检查之前打印了数据。

这是负责为我生成令牌和本地广播的方法:

@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);


    try {

        InstanceID instanceID = InstanceID.getInstance(this);
        token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

        Log.i(TAG, "GCM Registration Token: " + token);

        subscribeTopics(token);
        sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, true).apply();
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, false).apply();
    }

    Intent registrationComplete = new Intent(SessionManager.REGISTRATION_COMPLETE);
    System.out.println("token before assignment" + token);
    registrationComplete.putExtra("token", token.toString());
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    在 onReceive 方法中,您已经有一个意图作为参数。 所以而不是这个

       Bundle extras = getIntent().getExtras();
    

    试试这个

        Bundle extras = intent.getExtras();
    

    【讨论】:

    • 我明白了,但是一个和另一个有什么不同?
    • 那个,我想。您阅读了错误的附加内容;)我正在研究将捆绑包作为附加内容或直接将值作为附加内容放入意图之间是否存在差异。
    • 是的,这解决了它,但我还是不明白。
    • 我认为无论应用程序是否在前台运行,广播接收器总是等待呼叫。所以广播接收器直接从应用程序中获取意图。假设如果应用程序没有在前台运行 Intent 变为 null
    • getIntent().getExtras 调用整个类的意图,而 intent.getExtras 从方法参数中调用意图。
    猜你喜欢
    • 2015-02-27
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多