android:静态广播和动态广播

静态广播主要是在manifest.xml里面

加入 <receiver android:name=".MyBroadcastReceiver>组件。并给出接收类。

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.mybroadcast">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".MyBroadcastReceiver">

            <!-- 接收哪些广播 -->

            <intent-filter>
                <!-- 开机广播 -->
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <!-- 电量低广播 -->
                <action android:name="android.intent.action.BATTERY_LOW"/>

            </intent-filter>

        </receiver>

    </application>

</manifest>

 

接收类

package com.example.mybroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;


public class MyBroadcastReceiver extends BroadcastReceiver {
    final String TAG ="ygy";
    TextView mTextView;
    public MyBroadcastReceiver(TextView textView) {
        mTextView = textView;
    }
    public MyBroadcastReceiver() {

    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null){
            String action = intent.getAction();
            Log.e(TAG, "onReceive: "+ action );

            // 判断是什么广播(是不是我们自己发送的自定义广播)
            if(TextUtils.equals(action, MainActivity.MY_ACTION)){
                // 获取广播携带的内容, 可自定义的数据
                String content = intent.getStringExtra(MainActivity.BROADCAST_CONTENT);
                Log.e(TAG, "接收到的action是:"+ action + "\n接收到的内容是:\n" + content );
                if(mTextView != null){
                    mTextView.setText("接收到的action是:"+ action + "\n接收到的内容是:\n" + content);
                }
            }
        }
    }
}

动态广播,在activity里面注册了广播,然后在广播接收类里面接收。

package com.example.mybroadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    public static final String MY_ACTION = "com.ygy.demo.good";
    public static final String BROADCAST_CONTENT = "broadcast_content";
    private MyBroadcastReceiver mBroadcastReceiver;
    private EditText mInputEditText;
    private Button mSendBroadcastButton;
    private TextView mResultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 用包名做title
        setTitle(getPackageName());
        mInputEditText = findViewById(R.id.inputEditText);
        mSendBroadcastButton = findViewById(R.id.sendBroadcastButton);
        mResultTextView = findViewById(R.id.resultTextView);

        // 新建广播接收器
        mBroadcastReceiver = new MyBroadcastReceiver(mResultTextView);
       // mBroadcastReceiver = new MyBroadcastReceiver();
        // 注册广播接收器

        // 为广播接收器添加Action
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MY_ACTION);
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        intentFilter.addAction("android.intent.action.SCREEN_ON");

        //加上addDataScheme("package")就收不到其他消息了;不加,收不到 android.intent.action.PACKAGE_REMOVED
       // intentFilter.addAction("android.intent.action.PACKAGE_REMOVED");
       // intentFilter.addAction("android.intent.action.PACKAGE_INSTALL");
       // intentFilter.addDataScheme("package");

        // 注册广播接收器
        registerReceiver(mBroadcastReceiver, intentFilter);


        mSendBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 新建广播
                Intent intent = new Intent(MY_ACTION);
                // 放入广播要携带的数据
                intent.putExtra(BROADCAST_CONTENT, mInputEditText.getText().toString());
                sendBroadcast(intent);
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消注册广播接收器,不然会导致内存泄露
        if(mBroadcastReceiver != null){
            unregisterReceiver(mBroadcastReceiver);
        }
    }
}

按钮实现的是自定义广播发送。

android:静态广播和动态广播

 

android:静态广播和动态广播

上一篇:std:ios:sync_with_stdio (false)以及局限性


下一篇:android:系统服务、非绑定/绑定式服务的简单实例