简体   繁体   English

Android-将一个活动中的整数值传递给Android本机隐式意图并接收到另一个活动

[英]Android - Passing integer value from One Activity to Android Native implicit intent and recive to another activity

I have One Activity name AlbumPicker. 我有一个活动名称AlbumPicker。 In that activity I am calling below code on Button click. 在该活动中,我在Button click上调用以下代码。

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra("Example", 40);
((Activity) _ctx).startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        returnCode); 

Now Gallery will open. 现在画廊将打开。

Then i will select one image. 然后,我将选择一张图像。

then below method will get called I want Example value in below method 那么下面的方法将被调用我想要下面的方法中的示例值

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   int example = data.getIntegerExtra("Example);
}

but this code failes. 但是此代码失败。 I always recives 0 我总是念0

Please help 请帮忙

You don't have control over the data returned by the Gallery Activity, it's just return its own result intent, so your intent extra is lost. 您无法控制Gallery Activity返回的数据,它只是返回其自己的结果意图,因此您的意图额外损失了。 You should use an alternative way to get the value, maybe you can use something like this: 您应该使用另一种方式来获取价值,也许您可​​以使用如下方式:

final int yourReturnCode = 3040;
final int yourExampleValue = 40;
 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); ((Activity) _ctx).startActivityForResult( Intent.createChooser(intent, "Select Picture"), yourReturnCode); protected void onActivityResult(int requestCode, int resultCode, Intent data) { int yourValue = 0; if (resultCode == yourReturnCode) { yourValue = yourExampleValue; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM