广播

一个发布广播的类

 public void buttonBroadcaseStatic(View view){
        Intent intent = new Intent();
        //创建一个广播的名字
        intent.setAction("SB");
        sendBroadcast(intent);
       

 

一个接受广播的类

package com.example.zhuopeng.demo1;

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

public class BroadcaseReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
//        throw new UnsupportedOperationException("Not yet implemented");
        Toast.makeText(context,"static BroadcaseReceiver is here ",Toast.LENGTH_SHORT).show();
        //截断广播
        //abortBroadcast();
    }
}

注册

<receiver
            android:name=".BroadcaseReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="100"> <!-- 设置优先级,高优先级有权利截断广播 -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="SB" />
            </intent-filter>
        </receiver>

posted @ 2018-02-10 19:55  式微胡不归  阅读(114)  评论(0编辑  收藏  举报