美文网首页AndroidLearning
AndroidBroadcast发送与接收

AndroidBroadcast发送与接收

作者: nbpzjy | 来源:发表于2016-07-31 21:56 被阅读1361次

    学习Android中的Broadcast发送与接收
    在安卓中的四大组件,Activity,Service,BroadcastReceiver,ContentProvider
    广播用来传输数据,可用于在不同应用之间传递和共享数据或者同一应用的不同组件之间数据的共享,比如音乐播放器的UI更新等

    广播有普通广播,系统广播,有序广播,局部广播

    以下是学习中的源码,用两个应用,一个作为发送者,另外一个作为接收者。

    作为广播的发送者
    Manifest:

    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.nbp.broadcastesender">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            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=".MyFirstReceiver">
    
    
                <intent-filter android:priority="200">
    
                    <action android:name="com.nbp.broadcastereceiver"/>
                </intent-filter>
    
            </receiver>
        </application>
    
    </manifest>
    
    

    发送者的Mainactivity,写一个send()方法,Intent中包裹数据之后发送广播,普通广播sendBroadcast(intent),有序广播sendOrderedBroadcast(intent,null):

    MainActivity

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void send(View view){
    
            Intent intent = new Intent();
            //设置Action
            intent.setAction("com.nbp.broadcastereceiver");
            //Intent携带数据
            intent.putExtra("message","这里是广播消息");
            //发送广播--普通
            //sendBroadcast(intent);
            //发送有序广播
            sendOrderedBroadcast(intent,null);
            
    
        }
    }
    
    

    Activity_Main.xml,只放了一个按钮,用来调用send()方法来发送广播,onClick = send;

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.nbp.broadcastesender.MainActivity"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/main_send_broadcaste"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_send"
            android:onClick="send"/>
    </LinearLayout>
    

    在发送者内部的接收器,新建一个类来接收打印Log,只为验证广播用于同一应用中不同组件数据共享传输

    
    public class MyFirstReceiver extends BroadcastReceiver {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            Log.i("Broadcast_sender_1","Message details"+message);
        }
    }
    
    

    另外建一个工程作为接收广播者,以验证是不是跨进程接收,说明用于不同应用直接数据的共享过程:
    Manifest要这样写这里用的是静态注册,<receiver></receiver>中注册接收器,优先级设置在<intent-filter 后面, android:priority = xxx :

    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.nbp.broadcastreceiver">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            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=".MyIndipendentReceiver">
    
                <intent-filter android:priority="300">
                    <action android:name="com.nbp.broadcastereceiver"/>
                </intent-filter>
    
            </receiver>
    
            <receiver android:name=".MyIndipendentRvcTwo">
    
                <intent-filter android:priority="1000">
                    <action android:name="com.nbp.broadcastereceiver"/>
                </intent-filter>
    
            </receiver>
    
        </application>
    
    </manifest>
    
    

    <intent-filter android:priority="300">
    <intent-filter android:priority="1000">来定义接受>者的优先级别,数字越大,优先级越高,取值-1000到1000之间

    如果安卓以上的静态注册的话,在不需要界面时暂时不用Activity。如果要实现Activity内部动态注册的话,在onCreate()方法中按照如下方法注册,注意,动态注册的需要在onDestroy中取消注册unregisterReceiver(myIndipendentReceiver):

    public class MainActivity extends AppCompatActivity {
    
        MyIndipendentReceiver myIndipendentReceiver = new MyIndipendentReceiver();
        IntentFilter intentFilter = new IntentFilter();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //动态注册Broadcast receiver
            
    intentFilter.addAction("com.nbp.broadcastereceiver");
            registerReceiver(myIndipendentReceiver,intentFilter);
        }
    
        //第一行代码里面介绍说动态注册的Broadcast receiver一定要在destroy的时候取消注册
        @Override
        protected void onDestroy() {
            super.onDestroy();
            unregisterReceiver(myIndipendentReceiver);
        }
    }
    
    
    

    接收者中新建一个类,来实现第一个接收者

    public class MyIndipendentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            String message = intent.getStringExtra("message");
            Log.i("MyIndipendentRvcOne","Message details:"+message);
    
        }
    }
    
    

    再建一个类作为第二个接收者:

    
    
    public class MyIndipendentRvcTwo extends BroadcastReceiver {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            String message = intent.getStringExtra("message");
            Log.i("MyIndipendentRcvTwo","Message details:"+message);
    
        }
    }
    
    
    发送者内部的接收类打印的Log 接收者两个接收类,two的优先级大于one

    初学者正在努力,加油!

    相关文章

      网友评论

        本文标题:AndroidBroadcast发送与接收

        本文链接:https://www.haomeiwen.com/subject/nnhxsttx.html