赞
踩
步骤:
(1)创建 BroadOrderActivity,表示发送广播者
点击右下角的finish即完成创建
(BroadOrderActivity.java和在layout下的activity_broad_order.xml)
(2) 书写代码
这部分代码如下:
BroadOrderActivity.java
- package com.example.broad;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
-
- public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener {
-
- private BroadOrderReceiver broadOrderReceiver;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_broad_order);
- findViewById(R.id.btn_send_a).setOnClickListener(this);
- }
-
- @Override
- public void onClick(View view) {
- Intent intent = new Intent("com.example.broad.BroadOrderReceiver");
- sendOrderedBroadcast(intent,"com.example.broad.BroadOrderReceiver");
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- broadOrderReceiver = new BroadOrderReceiver();
- IntentFilter filter = new IntentFilter("com.example.broad.BroadOrderReceiver");
- registerReceiver(broadOrderReceiver,filter);
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- unregisterReceiver(broadOrderReceiver);
- }
- }
activity_broad_order.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <Button
- android:id="@+id/btn_send_a"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="发送广播"/>
-
- </LinearLayout>
(3)在同一个模块下创建一个接收者 BroadOrderReceiver
同样点击右下角的finish完成创建
(4)接收者代码如下(在控制台打印消息表示接收到~):
BroadOrderReceiver.java
- package com.example.broad;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class BroadOrderReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.d("nn","BroadOrderAReceiver接收到广播");
- }
- }
(5)重要的部分!我自己在这里纠结了好久,终于发现是这里的问题,才不能完成app之间通信
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android">
-
- <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
-
- <permission
- android:name="com.example.broad.BroadOrderReceiver"
- android:protectionLevel="signature"
- ></permission>
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/Theme.RecycleView">
- <receiver
- android:name=".BroadOrderReceiver"
- android:enabled="true"
- android:exported="true">
- <intent-filter>
- <action android:name="com.example.broad.BroadOrderReceiver"/>
- </intent-filter>
- </receiver>
-
- <activity
- android:name=".MainActivity"
- android:exported="false" />
-
-
- </receiver>
-
- <activity
- android:name=".BroadOrderActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
小小提一下,主要是这里要设置用户的权限(完整代码在上面,这里就是记录一下下):
- <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
-
- <permission
- android:name="com.example.broad.BroadOrderReceiver"
- android:protectionLevel="signature"
- ></permission>
(1)创建模块
点击finish就完成模块的创建
(会自动生成MainActivity.java 和 activity_main.xml,只修改了MainActivity.java)
代码如下:
MainActivity.java
- package com.example.broadb;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.IntentFilter;
- import android.os.Bundle;
-
- public class MainActivity extends AppCompatActivity {
-
- private BroadcastBReceiver broadcastBReceiver;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- broadcastBReceiver = new BroadcastBReceiver();
- IntentFilter intentFilter = new IntentFilter("com.example.broad.BroadOrderReceiver");
- registerReceiver(broadcastBReceiver,intentFilter);
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- unregisterReceiver(broadcastBReceiver);
- }
- }
(2)创建 BroadcastBReceiver
创建上一个模块一样~
这里就直接附上代码
BroadcastBReceiver.java
- package com.example.broadb;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class BroadcastBReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.d("nn","BroadcastBReceiver收到广播");
- }
- }
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android">
- <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
- <permission android:name="com.example.broad.BroadOrderReceiver"></permission>
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/Theme.RecycleView">
- <receiver
- android:name=".BroadcastBReceiver"
- android:enabled="true"
- android:exported="true">
- <intent-filter android:priority="100">
- <action android:name="com.example.broad.BroadOrderReceiver"/>
- </intent-filter>
- </receiver>
-
- <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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。