[英]Get data from another application in android
我安装了两个应用程序app1和app2。 我想从app1启动app2的活动。 然后使用来自app2的一些数据返回到app1上的活动。 但是我能够返回到app1,但是无法获取数据。
来自app1:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, sampleText);
intent.setType("text/plain");
intent.setComponent(new ComponentName("com.example.app2","com.example.app2.MainActivity"));
startActivity(intent);
来自app2:
Intent goBack = new Intent();
goBack.putExtra("result","asdaf");
setResult(RESULT_OK, goBack);
finish();
app1的清单:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
app2的清单:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
我没有在app1中获得成功。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
TextView tv = (TextView)findViewById(R.id.textView3);
tv.setText(result);
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
我在这里想念什么? 是否需要为此进行任何设置?
您应该在“ app1”中使用startActivityForResult:
startActivityForResult(intent, SOME_CONSTANT);
您传递的常数将作为requestCode参数传回。 因此,在您的情况下,您似乎希望传递1。
请参阅文档以获取更多信息: Android开发人员
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.