Java 从android中的其他应用程序获取数据
我安装了两个应用程序app1和app2。 我想从app1启动app2的活动。然后返回到app1上的活动,其中包含来自app2的一些数据。但我可以返回到app1,但无法获取数据 从app1:Java 从android中的其他应用程序获取数据,java,android,android-intent,android-manifest,Java,Android,Android Intent,Android Manifest,我安装了两个应用程序app1和app2。 我想从app1启动app2的活动。然后返回到app1上的活动,其中包含来自app2的一些数据。但我可以返回到app1,但无法获取数据 从app1: Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, sampleText); intent.setType(
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>
我错过了什么?。是否需要对此进行任何设置?您应该在“app1”中使用startActivityForResult: 您传递的常量将作为requestCode参数传回。所以在你的情况下,看起来你想通过1级考试 有关更多信息,请参阅文档:
<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>
@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
}
}
}
startActivityForResult(intent, SOME_CONSTANT);