[英]How to send a string between two own applications?
我有两个自己的应用程序,App A和App B. App A的活动,通过传递String打开App 2。
代码App1:
Intent launchIntent = getMainActivity().getPackageManager().getLaunchIntentForPackage("com.example.app2");
if (launchIntent != null)
{
launchIntent.setAction(Intent.ACTION_SEND);
launchIntent.putExtra("stringApp1ToApp2", "myString");
launchIntent.setType("text/plain");
startActivity(launchIntent);
}
代码App2:
Bundle parameters = this.getIntent().getExtras();
if(parameters != null)
{
String b = parameters.getString("stringApp1ToApp2", "stringDefault");
}
工作正常。
我的问题是当我想从App2发送一个String到App1。 在应用程序2中有一个按钮,当您单击时,您必须关闭应用程序(finish())并将字符串发送到应用程序1.但是不要从头开始打开App1 ..
有任何想法吗?
先感谢您。
我相信你需要在app 1中的mainactivity中覆盖方法OnNewIntent,这是一个新的意图而不是startintent
您可以使用intent过滤器来实现此目的。 在清单中定义app1的活动,如下所示。
<activity
android:name=".App1Activity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="send_data"
android:scheme="app1" />
</intent-filter>
</activity>
你可以使用app2从app2启动App1Activity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("app1://send_data?string_App2_To_App1=myString"));
try {
startActivity(intent);
} catch (e: Exception) {
Util.showToast(this, "Activity not found");
}
现在,您可以使用Activity App1Activity的onCreate / onNewIntent()中的代码将app2发送到app1的数据
Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
String stringFromApp2 = data.getQueryParameter("string_App2_To_App1");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.