字符串参数未从本地通知发送到活动

时间:2019-06-28 14:22:49

标签: android android-intent alarmmanager localnotification

我目前正在尝试发送带有操作的本地通知。选中后,该操作会将字符串推送到活动。不幸的是,当我尝试传递一个字符串时,我无法在活动中接收它,我有多个带有不同对象的日志语句,以查看系统是否将其识别为别的东西,但是没有运气。当我尝试将其作为捆绑包发送时发生的事件是,捆绑包被发送了,但字符串却没有发送!如果我通过一个int,那就很好!任何帮助将不胜感激,是的,我一直在为此进行挖掘,并尝试了多种解决方案。

代码:

Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setAction("action");

        intent.putExtra("idforaction", 12345);
        //intent.putExtra("test", "test message");
        Bundle bundle = new Bundle();
        bundle.putString("othertest","test message 2");
        intent.putExtra("test", bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

MainActivity:

int notificationNum = getIntent().getIntExtra(idfoaction, -1);
        Log.e(TAG, "get Extras " + notificationNum);

        Log.e(TAG, "STRING? " + getIntent().getStringExtra("test"));
        Log.e(TAG, "CHARSEQ? " + getIntent().getCharSequenceExtra("test"));
        Log.e(TAG, "CHARS?" + (getIntent().getCharArrayExtra("test") != null ? getIntent().getCharArrayExtra("test").length + "" : "null"));
        Log.e(TAG, "PARCELABLE?" + (getIntent().getParcelableExtra("test") != null ? getIntent().getParcelableExtra("test").toString(): "null"));
        Log.e(TAG, "BUNDLE?" + (getIntent().getBundleExtra("test") != null ? "not null" : "null"));
        Log.e(TAG, "BUNDLE?" + (getIntent().getExtras() != null ? "not null" : "null"));
        if (getIntent().getExtras() != null) {
            Log.e(TAG, "bundle " + getIntent().getExtras().getString("othertest", ""));
            Log.e(TAG, "bundle " + getIntent().getExtras().get("othertest"));
        }

1 个答案:

答案 0 :(得分:1)

尝试一下:

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("my_key", "my_key_value");

然后阅读:

Intent intent = getIntent()
String myValue = intent.getStringExtra("my_key"); // "my_key_value"