解決安卓自定義廣播沒有響應問題以及不同程序間接收廣播


最近在學習安卓的廣播,其中根據書上自定義廣播的代碼運行后卻沒有反應,百度后知道了解決方案,記錄下來:

 

一、同一包內自定義廣播

1.首先新建一個廣播接收器類MyBroadcastReceiver.java

File-new-other-Brodecast Receiver,然后在彈出框輸入廣播接收器的類名

 

 

2.修改MyBroadcastReceiver.java代碼

package com.example.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"receiver in MyBroadercastReceiver",Toast.LENGTH_SHORT).show();

    }
}

 

 

3.修改AndroidManifest.xml代碼

其中第5、6行、第19-21行是我自己加上去的,其他不用原本就有。

修改后可以看到,我們讓MyBroadcastReceiver廣播接收器接受一條com.example.broadercasttest.MY_BROADCAST的廣播,因此我們需要發送一條這樣的廣播

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.broadcasttest">
 4 
 5     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 6     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 7 
 8     <application
 9         android:allowBackup="true"
10         android:icon="@mipmap/ic_launcher"
11         android:label="@string/app_name"
12         android:roundIcon="@mipmap/ic_launcher_round"
13         android:supportsRtl="true"
14         android:theme="@style/AppTheme">
15         <receiver
16             android:name=".MyBroadcastReceiver"
17             android:enabled="true"
18             android:exported="true">
19             <intent-filter>
20                 <action android:name="com.example.broadercasttest.MY_BROADCAST"/>
21                 <category android:name="android.intent.category.DEFAULT"/>
22             </intent-filter>
23         </receiver>
24         <activity android:name=".MainActivity">
25             <intent-filter>
26                 <action android:name="android.intent.action.MAIN" />
27                 <category android:name="android.intent.category.LAUNCHER" />
28             </intent-filter>
29         </activity>
30     </application>
31 
32 </manifest>

 

 

4.修改activity_main.xml,添加一個發送按鈕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="發送廣播"/>
</LinearLayout>

 

 

5.修改MainActivity.java,給按鈕設置點擊事件

      首先構建一個Intent對象,把要發送的廣播值傳入intent,而setComponent的方法就是告訴系統你該發給誰,換句話說就是誰接受廣播,然后Content的sendBroadcast()方法將廣播發送出去,這樣所有監聽 com.example.broadercasttest.MY_BROADCAST 這條廣播的廣播接收器就會收到消息,調用onReceive()方法。

 1 package com.example.broadcasttest;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.BroadcastReceiver;
 6 import android.content.ComponentName;
 7 import android.content.Context;
 8 import android.content.Intent;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.Toast;
13 
14 public class MainActivity extends AppCompatActivity {
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         Button button=(Button)findViewById(R.id.button);
20         button.setOnClickListener(new View.OnClickListener() {
21             @Override
22             public void onClick(View v) {
23                 Intent intent=new Intent("com.example.broadercasttest.MY_BROADCAST");
24                 intent.setComponent(new ComponentName(getPackageName(),"com.example.broadcasttest.MyBroadcastReceiver"));
25                 sendBroadcast(intent);
26             }
27         });
28 }

 

如果沒有加 intent.setComponent(new ComponentName(getPackageName(),"com.example.broadcasttest.MyBroadcastReceiver")); 這一句,程序運行后是沒有響應的。

第一個參數是目標廣播接收器所在應用的包名,第二個參數是目標廣播接收器類全路徑(即MyBroadcastReceiver類的路徑)。

 

 

 

二、不同包內接收廣播

即BrodecastTest程序發出的廣播是可以被其他的應用程序接收到的

1.新建一個項目BrodecastTest2,然后新建一個廣播接收器AnotherBrodecastReceiver.java

修改AnotherBrodecastReceiver.java

 1 package com.example.brodecasttest2;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.widget.Toast;
 7 
 8 public class AnotherBrodecastReceiver extends BroadcastReceiver {
 9     @Override
10     public void onReceive(Context context, Intent intent) {
11         Toast.makeText(context,"receive in AnotherBrodecastReceiver",Toast.LENGTH_SHORT).show();
12 
13     }
14 }

 

 

2.修改AndroidManifest.xml

在receiver加入<action android:name="com.example.broadcasttest.MY_BROADCAST"/>,說明AnotherBrodecastReceiver接收器也是接收com.example.broadcasttest.MY_BROADCAST這條廣播

<receiver
            android:name=".AnotherBrodecastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
 </receiver>

 

 

3.回到BrodecastTest項目,修改MainActivity.java

intent.setComponent(new ComponentName(getPackageName(),"com.example.broadcasttest.MyBroadcastReceiver"))修改為 intent.addFlags(0x01000000);會有紅色下划線,但是不用理會。

 

4.依次運行兩個程序,再回到手機里BrodecastTest軟件的界面,點擊發送廣播按鈕,會彈出兩次提示信息

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM