ChatGPT解决这个技术问题 Extra ChatGPT

如何从 Android 上的意图中获取额外数据?

如何将数据从一项活动(意图)发送到另一项活动?

我使用此代码发送数据:

Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
Java 旁注:像这样“字符串化”整数从来都不是一个好主意(尤其是出于示例目的),不幸的是,它经常被认为是在 java:user.getUserAccountId()+"" 中将 int 转换为字符串的一种很好、快速的方法,因为这样会创建要收集的不必要的对象。考虑改用 String.valueOf(user.getUserAccountId)Integer.toString(user.getUserAccountId)
@Andrew S 这不是网络吗?这是“从意图获取数据”的第一结果
@AndrewS 我同意麦奎尔的观点。另外,这个问题是不久前发布的,所以当时可能不太容易找到答案。如果还没有向 SO 发布类似的问题,那么它是一个有效的帖子。

M
Malcolm

首先,使用 getIntent() 方法获取启动您的活动的意图:

Intent intent = getIntent();

如果您的额外数据表示为字符串,那么您可以使用 intent.getStringExtra(String name) 方法。在你的情况下:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

我从哪里可以使用所有这些方法??
@adham:如果您在活动中,从 onCreate 中调用 getIntent().getStringExtra("id"); 以获取 id 字符串
您可以通过调用 getIntent() 方法获取启动活动的意图。我已经更新了答案。
@Eatlon如果您对特定库有疑问,您应该为此创建一个单独的问题。
@MelColm getExtra().getString 和 getStringExtra() 有什么区别?
N
NickT

在接收活动中

Bundle extras = getIntent().getExtras(); 
String userName;

if (extras != null) {
    userName = extras.getString("name");
    // and get whatever type user account id is
}

为什么这比 getStringExtra? 更可取
我的猜测是:如果 extras 可以是 null,则可以跳过整个 extras 提取。通过使用 getStringExtra,您基本上可以将其更改为一系列 if(extras != null) { return extras.getString(name) }。您调用的每个 getStringExtra 对应一个。此选项将检查一次 null,如果是,则根本不会阅读 Bundle。除此之外,getStringExtra 也可能每次都在内部继续询问 getExtras。因此,您只需对函数进行更多调用。
Q
Qassim babyDroid
//  How to send value using intent from one class to another class
//  class A(which will send data)
    Intent theIntent = new Intent(this, B.class);
    theIntent.putExtra("name", john);
    startActivity(theIntent);
//  How to get these values in another class
//  Class B
    Intent i= getIntent();
    i.getStringExtra("name");
//  if you log here i than you will get the value of i i.e. john

k
kenju

加起来

设置数据

String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);

获取数据

String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    value = bundle.getString("sample_name");
}

P
Peter Mortensen

无需初始化另一个新的 Intent 来接收数据,只需执行以下操作:

String id = getIntent().getStringExtra("id");

i
ib.

按意图放置数据:

Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);

按意图获取数据:

String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);

如果我们为意图使用整数值,我们必须在 getIntent().getIntExtra("instituteId", 0) 中将第二个参数设置为 0。否则,我们不使用 0,Android给我一个错误。


P
Peter Mortensen

如果在 FragmentActivity 中使用,请尝试以下操作:

第一个页面扩展了 FragmentActivity

Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);

在片段中,您只需要先调用 getActivity()

第二页扩展了 Fragment:

String receive = getActivity().getIntent().getExtras().getString("name");

您也可以使用 getStringExtra("name") 代替 getExtras().getString("name")
A
Arsen Khachaturyan

如果您试图在片段中获取额外数据,那么您可以尝试使用:

使用以下方式放置数据:

Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);

使用以下方式获取数据:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


  getArguments().getInt(ARG_SECTION_NUMBER);
  getArguments().getString(ARG_SECTION_STRING);
  getArguments().getBoolean(ARG_SECTION_BOOL);
  getArguments().getChar(ARG_SECTION_CHAR);
  getArguments().getByte(ARG_SECTION_DATA);

}

C
Community

科特林

第一个活动

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)

第二次活动

val value = getIntent().getStringExtra("key")

建议

始终将密钥放在常量文件中以获得更多管理方式。

companion object {
    val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}

s
shobhan

您可以从意图中获取任何类型的额外数据,无论它是对象、字符串还是任何类型的数据。

Bundle extra = getIntent().getExtras();

if (extra != null){
    String str1 = (String) extra.get("obj"); // get a object

    String str2 =  extra.getString("string"); //get a string
}

最短的解决方案是:

Boolean isGranted = getIntent().getBooleanExtra("tag", false);

P
Peter Mortensen

只是一个建议:

我建议不要在 i.putExtra("id".....) 中使用“id”或“name”,而是在有意义时使用可与 putExtra() 一起使用的当前标准字段,即Intent.EXTRA_something。

完整列表可在 Intent(Android 开发者)中找到。


s
sam

我们可以通过简单的方式做到这一点:

在第一个活动中:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);

在第二活动中:

    try {
        Intent intent = getIntent();

        String uid = intent.getStringExtra("uid");
        String pwd = intent.getStringExtra("pwd");

    } catch (Exception e) {
        e.printStackTrace();
        Log.e("getStringExtra_EX", e + "");
    }

R
Ruzin

在 First Activity 上传递带有值的意图:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);

接收第二个活动的意图;-

Intent intent = getIntent();
String user = intent.getStringExtra("uid");
String pass = intent.getStringExtra("pwd");

我们通常使用两种方法来发送值和获取值。为了发送值,我们将使用 intent.putExtra("key", Value); 并且在接收另一个活动的意图期间,我们将使用 intent.getStringExtra("key"); 来获取意图数据作为 String 或使用其他可用方法来获取其他类型的数据(Integer、{ 5}等)。关键可以是任何关键字来识别价值意味着你正在分享什么价值。希望它对你有用。


S
Savita Sharma

你也可以这样做 // 把价值放在意图中

    Intent in = new Intent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);

// 从意图中获取值

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String filter = bundle.getString("filter");

R
Rohit Singh

从 Intent 中获取不同类型的 Extra

要从 Intent 访问数据,您应该知道两件事。

钥匙

数据的数据类型。

Intent 类中有不同的方法来提取不同类型的数据类型。看起来像这样

getIntent().XXXX(KEY) 或 intent.XXX(KEY);

因此,如果您知道在 otherActivity 中设置的变量的数据类型,则可以使用相应的方法。

从 Intent 中检索 Activity 中的字符串的示例

String profileName = getIntent().getStringExtra("SomeKey");

不同数据类型的不同方法变体列表

您可以在 Intent 的官方文档中查看可用方法的列表。


A
Android Geek

这适用于适配器,对于活动,您只需将 mContext 更改为您的 Activty 名称,对于片段,您需要将 mContext 更改为 getActivity()

 public static ArrayList<String> tags_array ;// static array list if you want to pass array data

      public void sendDataBundle(){
            tags_array = new ArrayList();
            tags_array.add("hashtag");//few array data
            tags_array.add("selling");
            tags_array.add("cityname");
            tags_array.add("more");
            tags_array.add("mobile");
            tags_array.add("android");
            tags_array.add("dress");
            Intent su = new Intent(mContext, ViewItemActivity.class);
            Bundle bun1 = new Bundle();
            bun1.putString("product_title","My Product Titile");
            bun1.putString("product_description", "My Product Discription");
            bun1.putString("category", "Product Category");
            bun1.putStringArrayList("hashtag", tags_array);//to pass array list 
            su.putExtras(bun1);
            mContext.startActivity(su);
        }