扣丁学堂——BroadcastReceiver(广播)

源码见本文

一丶测试效果:略

二丶课程讲解

扣丁学堂——BroadcastReceiver(广播)_第1张图片

扣丁学堂——BroadcastReceiver(广播)_第2张图片



1.普通广播代码:

广播接收器:

public class MyReceiver01 extends BroadcastReceiver {
    public MyReceiver01() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        String i = intent.getStringExtra("info");
        Toast.makeText(context, i, Toast.LENGTH_SHORT).show();
    }
}
2.Activity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendNormal(View v) {
        Intent intent = new Intent("com.ke.action.MY_BROADCAST");//action字符相当于广播的频率一样
        intent.putExtra("info", "this is boradcast");//要发送广播的内容
        this.sendBroadcast(intent);//发送广播
    }
}
3.清单文件

<receiver
    android:name=".MyReceiver01"
    android:enabled="true"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="com.ke.action.MY_BROADCAST"></action>
    </intent-filter>
扣丁学堂——BroadcastReceiver(广播)_第3张图片

扣丁学堂——BroadcastReceiver(广播)_第4张图片

上面是静态注册,这里就说一下动态注册

广播接收器2

public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"动态接受广播",Toast.LENGTH_SHORT).show();
    }
}
Activity.java

public class MainActivity extends AppCompatActivity {
    private MyReceiver2 receiver2=new MyReceiver2();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//该方法注册广播
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.ke.action.MY_BROADCAST");
        registerReceiver(receiver2,filter);
    }
//该方法解除广播
    @Override
    protected void onPause() {
        unregisterReceiver(receiver2);
        super.onPause();
    }
这里效果演示是先输出动态广播

扣丁学堂——BroadcastReceiver(广播)_第5张图片


MainActivity.java

public void sendOrderClick(View v) {
    Intent intent = new Intent("com.andorid.keke");
    //参数:intent ,接收权限:接收广播接收器需的权限
    this.sendOrderedBroadcast(intent, null);
}
MyReceiver3

public class MyReceiver3 extends BroadcastReceiver {
    public MyReceiver3() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "有序广播接收器-1", Toast.LENGTH_SHORT).show();
        //中断有序广播
        //this.abortBroadcast();
        Bundle data = new Bundle();
        data.putString("info","广播1");
        this.setResultExtras(data);//将数据发送到下一个广播接收器中
    }
}
MyReceiver4

public class MyReceiver4 extends BroadcastReceiver {
    public MyReceiver4() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle data =  getResultExtras(false);// false:没有的话就不创建
        Toast.makeText(context, data.getString("info").toString(), Toast.LENGTH_SHORT).show();

    }
}
清单文件

<receiver
    android:name=".MyReceiver3"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="200">
        <action android:name="com.andorid.keke"></action>
    </intent-filter>

</receiver>
<receiver
    android:name=".MyReceiver4"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="100">
        <action android:name="com.andorid.keke"></action>
    </intent-filter>
</receiver>
扣丁学堂——BroadcastReceiver(广播)_第6张图片

演示了一个跳转发送例子略


扣丁学堂——BroadcastReceiver(广播)_第7张图片

扣丁学堂——BroadcastReceiver(广播)_第8张图片


关于广播后期再加强,这里只是提到基本操作。


你可能感兴趣的:(扣丁学堂——BroadcastReceiver(广播))