gpt4 book ai didi

android - 从服务启动的 Activity 丢失了包中的 "extra"

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:05 27 4
gpt4 key购买 nike

调用代码(在服务中运行):

Intent textIntent = new Intent(this, TextActivity.class);
textIntent.putExtra("text_seq", message.xfer.seq);
textIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(textIntent);

调用代码(在TextActivity中):

@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.d(TAG, "" + bundle.getInt("text_seq"))
...

事实上,整个包都丢失了 - 上面的代码在调用 bundle.getInt() 时抛出 NPE。

我确定我错过了一些明显的东西......

最佳答案

您正在阅读的 Bundle 不是为了这个目的。作为per docs

void onCreate (Bundle savedInstanceState)

Bundle: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

如果您需要额外的服务,您需要调用:

Bundle extras = getIntent().getExtra();

然后您可以尝试获取您的值:

int myVal = extras.getInt(key);

或者你可以尝试使用:

int myVal = getIntent().getIntExtra(key, defaultVal);

关于android - 从服务启动的 Activity 丢失了包中的 "extra",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com