将值从应用程序传递到另一个应用程序

时间:2018-05-27 15:08:32

标签: android

我有2个单独的应用程序。从App1,我需要将位置/目的地传递给App2,以便它将在弹出窗口中显示

任何人都可以帮助我吗?

我尝试过Intents,SharedPreferences甚至尝试了解ContentProviders(不好)。似乎没什么用。

已经有很长一段时间了,但还没有能够做到这一点。

修改

代码使用....

App1:com.myapp.rider - 从rider app发送到驱动程序应用

Intent intent = new Intent("com.myapp.driver.Activities.DriverTracking");
intent.putExtra("destination", destination);
startActivity(intent);

App2:com.myapp.driver - 从骑手接收价值

Bundle bundle = getIntent().getExtras();
mDestination = (String) bundle.get("destination");
Log.d("STATUS_1", "Destination = " + mDestination);

在App2中添加到Manifest:

<intent-filter>
   <action android:name="com.myapp.driver.Activities.DriverTracking" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

以上我尝试过以下错误:

 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.myapp.driver.Activities.DriverTracking (has extras) }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1878)

图片 enter image description here

已编辑 - 错误:

05-27 14:45:12.940 21851-21851/com.myapp.rider E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.rider, PID: 21851
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.rider/com.myapp.rider.Activities.RiderHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtras(android.os.Bundle)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtras(android.os.Bundle)' on a null object reference
    at com.myapp.rider.Activities.RiderHome.sendIntent(RiderHome.java:355)

2 个答案:

答案 0 :(得分:0)

修复了Intent + Bundle + PackageManager的解决方案:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.myapp.driver", "com.myapp.driver.Activities.DriverTracking"));
intent.putExtra("destination", destination);
startActivity(intent);

在第二个App中的Activity中,您需要获取Bundle值:

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

答案 1 :(得分:0)

  

App1:com.myapp.rider - 从rider app发送到驱动程序应用程序

您正在设置一个Intent,期望有一些<activity> <intent-filter> <action> com.myapp.driver.Activities.DriverTracking

  

在App2中添加到Manifest:

此处,您的操作为com.myapp.ryyde_driver.Activities.DriverTracking。这与您的Intent不符。您需要将Intent中的操作与<action>中的<intent-filter>相匹配。

相关问题