这是我的代码:
发件人应用
MainActivity.java
package com.karthik.apptx;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnClickButton(View view) {
Intent intent = new Intent();
intent.setAction("com.karthik.apprx");
intent.putExtra("PACKAGE_NAME", this.getPackageName());
intent.putExtra("PID", Process.myPid());
intent.putExtra("DATA", "Hello Karthik");
intent.setComponent(new ComponentName("com.karthik.apprx", "com.karthik.apprx.AppRxReceiver"));
sendBroadcast(intent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karthik.apptx">
<uses-permission android:name="com.karthik.PERMISSION"/>
<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/Theme.AppTx">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
接收器应用程序(用 Kotlin 编写)
MyBroadcastReceiver.kt
package com.karthik.apprx
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class AppRxReceiver : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
Log.d("AppRxReceiver", intent.toString())
Log.d("AppRxReceiver", intent?.getStringExtra("PACKAGE_NAME").toString())
Log.d("AppRxReceiver", "" + intent?.getIntExtra("PID", 0))
Log.d("AppRxReceiver", intent?.getStringExtra("DATA").toString())
Toast.makeText(ctx, "Received Data", Toast.LENGTH_SHORT).show()
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karthik.apprx">
<permission
android:name="com.karthik.PERMISSION"
android:protectionLevel="signature"/>
<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/Theme.AppRx">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.AppRx.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AppRxReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.karthik.PERMISSION">
<intent-filter>
<action android:name="com.karthik.apptx" />
</intent-filter>
</receiver>
</application>
</manifest>
PS:为方便起见,我选择了用 Java 编写 Sender-App 和 Kotlin 编写 Receiver-App。