如何在不使用ContentProvider或sharedPreference

时间:2017-10-11 13:49:09

标签: java android

appA mainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnSendBroadcast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
        btnSendBroadcast.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        final Intent intent=new Intent();
        intent.setAction("com.example.admin.chromium");
        intent.putExtra("KeyName","code1id");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.setComponent(
                new ComponentName("com.example.admin.chromiumsendmessage","com.example.admin.chromiumsendmessage.MainActivity"));
        sendBroadcast(intent);
        Toast.makeText(getApplicationContext(), "KeyName value sent to ChromiumSendMessage app" , Toast.LENGTH_SHORT).show();

    }
}

appB manifest--

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.chromiumsendmessage">
    <uses-permission android:name="android.permission.SEND_SMS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <receiver android:name=".MyBroadcast"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.admin.chromium" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

appB接收器类:

public class MyBroadcast extends BroadcastReceiver {
    String msg;
    String name;
    @Override
    public void onReceive(Context context, Intent intent) {


   msg = intent.getStringExtra("KeyName");

   name= msg;

        Toast.makeText(context, "value  - " + name, Toast.LENGTH_SHORT ).show();

        }
}

AppB manifest appA mainActivity

有两个应用程序,appA&amp;程序appB。在appA中有一个按钮。在appA中,如果我点击该按钮,它应该向appB发送一些值(String/Integer)而不使用ContentProviderSharedPreference。此外,appB应接收其“BroadcastReceiver类中的值。有人可以帮帮我吗?

我已经从appB添加了appA MainActivity代码和Receiver类以及Manifest。

1 个答案:

答案 0 :(得分:0)

您可以使用sendBroadcast()方法将String值从一个应用发送到另一个应用。

但是,首先您需要在Manifest注册您的接收器应用程序。

例如,假设您有两个应用, App1 App2

App1 会发送字符串值, App2 会收到该值。

App1 中,代码应该是这样的,

final Intent intent=new Intent();
intent.setAction("com.pkg.App1");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(  
    new ComponentName("com.pkg.App2","com.pkg.App2.MyBroadcastReceiver"));  
sendBroadcast(intent);

要在 App2 中接收此广播消息,请在 App2 Manifest中编写代码,如下所示:

<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
    <intent-filter>
      <action android:name="com.pkg.App1" />
    </intent-filter>
</receiver>
  

FLAG_INCLUDE_STOPPED_PACKAGES标志被添加到之前的意图中   发送以表明允许启动组件的意图   停止申请。