欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义广播

程序员文章站 2024-03-17 11:07:16
...

一、静态注册广播

//自定义类继承姿BroadcastReceiver
//清单文件注册广播
public class My_Recevier extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.xxs.666")){
            String data = intent.getStringExtra("data");
            Toast.makeText(context, data, Toast.LENGTH_SHORT).show();
        }
    }

清单文件

<receiver android:name=".My_Recevier">
    <intent-filter>
        <action android:name="com.xxs.666"></action>
    </intent-filter>
</receiver>

发送广播

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction("com.xxs.666");
        intent.putExtra("data","李狗蛋你家猪跑了");
        sendBroadcast(intent);
    }
});

二、动态注册广播

两个方法
registerReceiver();注册
unregisterReceiver();解除注册

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = findViewById(R.id.text);
     my_recevier = new My_Recevier();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.xxs.777");
    //写registerReceiver()方法 参数一 自定义接收者 参数二 IntentFilter 添加频道
    registerReceiver(my_recevier,intentFilter);
}
//在activity生命周期结束之后注销注册
@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(my_recevier);
}

三、系统广播广播

public class My_Reciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {//电量发生改变
            //获取当前电量
            int level = intent.getIntExtra("level", 0);
            Toast.makeText(context, "当前电量: " + level, Toast.LENGTH_SHORT).show();
            Log.d("ytx", "当前电量: " + level);
        } else if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {//低电量
            Toast.makeText(context, "电量低: ", Toast.LENGTH_SHORT).show();
            Log.d("ytx", "电量低: ");
        } else if (intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {//电量充足
            Toast.makeText(context, "电量充足: ", Toast.LENGTH_SHORT).show();
            Log.d("ytx", "电量充足: ");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {//亮屏
            Toast.makeText(context, "亮屏: ", Toast.LENGTH_SHORT).show();
            Log.d("ytx", "亮屏: ");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {//锁屏
            Toast.makeText(context, "锁屏: ", Toast.LENGTH_SHORT).show();
            Log.d("ytx", "锁屏: ");
        } else if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {//飞行
            Bundle extras = intent.getExtras();
            int state = extras.getInt("state");
            if (state == 0) {
                Toast.makeText(context, "飞行关闭: ", Toast.LENGTH_SHORT).show();
            } else if (state == 2) {
                Toast.makeText(context, "飞行打开: ", Toast.LENGTH_SHORT).show();
            }
        }else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)){//插入耳机
            Toast.makeText(context, "插入耳机: ", Toast.LENGTH_SHORT).show();
        }
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
    intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
    intentFilter.addAction(Intent.ACTION_SCREEN_ON);
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
    My_Reciver my_reciver = new My_Reciver();
    registerReceiver(my_reciver,intentFilter);
}