gpt4 book ai didi

android - 如何在不打开另一个 Activity 的情况下通过 Intent 在android中发送数据?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:01 27 4
gpt4 key购买 nike

这是我按 Intent 发送数据的代码,但我不想打开另一个 Activity 我只想发送数据而不打开它..

Bundle contain = new Bundle();
contain.putString("key4", item);
contain.putString("price", price);

Intent a = new Intent(Searchbydate.this, Searchbyitem.class);
a.putExtras(contain);
startActivity(a);

这里我不想打开这个 Searchbyitem.class 只是发送数据...

最佳答案

是的,我也遇到过这个问题。

许多开发人员在通过 Intent 或 Bundle 将数据从对话框传递到另一个 Activity 时也会遇到问题。它在从另一个 Activity 中检索时返回 null。

唯一的解决方案是 SharedPreferences。

但你必须将它放在关闭按钮内。(例如:确定/取消等)

并通过同一个键轻松地从另一个 Activity 中检索数据。不要使用任何带有广播 Intent 的服务。

对话 Activity 中的代码是这样的:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.mailicon);
builder.setTitle(name);
builder.setView(view);

builder.setPositiveButton("Send Request",new DialogInterface.OnClickListener()
{

@Override
public void onClick(DialogInterface dialog, int which) {
String mailID = id.getText().toString();

//Here define all your sharedpreferences code with key and value
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("MID", mailID );
edit.commit();

}

});

然后像这样从另一个获取数据:

    SharedPreferences bb = getSharedPreferences("my_prefs", 0);
String m = bb.getString("NUM", "");
Toast.makeText(this, m, Toast.LENGTH_SHORT).show();

为良好标准添加一些检查。

谢谢

关于android - 如何在不打开另一个 Activity 的情况下通过 Intent 在android中发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466673/

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