Android 广播 使用方法

动态注册的广播接收器

// MainActivity.java

public class MainActivity extends AppCompatActivity {

    private IntentFilter mIntentFilter;
    private DynamicBroadcastReceiver mDynamicBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ....

        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("THIS_IS_A_DYNAMIC_BROADCAST");
        mDynamicBroadcastReceiver = new DynamicBroadcastReceiver();
        // 动态注册广播接收器
        registerReceiver(mDynamicBroadcastReceiver, mIntentFilter);
        // 发送广播
        Intent intent = new Intent("THIS_IS_A_DYNAMIC_BROADCAST");
        sendBroadcast(intent);
    }

    @Override
    protected void onDestroy() {

        ....

        unregisterReceiver(mDynamicBroadcastReceiver);
    }

    class DynamicBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("Guu", "receive dynamic broadcast!");
        }
    }

}

静态注册的广播接收器

// AndroidManifest.xml

"http://schemas.android.com/apk/res/android"
          package="com.example.broadcast">

    "android.permission.RECEIVE_BOOT_COMPLETED" />

    // 静态注册广播接收器
        ".StaticBroadcastReceiver">
            
                "THIS_IS_A_STATIC_BROADCAST" />
            
        
    



// StaticBroadcastReceiver.java

public class StaticBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Guu", "receive static broadcast!");
    }

}

// MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ....

        // 发送广播
        intent = new Intent("THIS_IS_A_STATIC_BROADCAST");
        sendBroadcast(intent);
    }

}

发送有序广播

1. 发送有序广播的方法:

intent = new Intent("THIS_IS_A_STATIC_BROADCAST");
sendOrderedBroadcast(intent, null);

2. 设置广播接收器的优先级:

// 静态注册
filter android:priority="100">

// 动态注册
mIntentFilter.setPriority(100);

3. 截断广播的方法:

public void onReceive(Context context, Intent intent) {

    ....

    abortBroadcast();
}

使用本地广播

// MainActivity.java

public class MainActivity extends AppCompatActivity {

    private IntentFilter mIntentFilter;
    private DynamicBroadcastReceiver mDynamicBroadcastReceiver;
    private LocalBroadcastManager mLocalBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ....

        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("THIS_IS_A_DYNAMIC_BROADCAST"); 
        mDynamicBroadcastReceiver = new DynamicBroadcastReceiver();
        // 使用 LocalBroadcastManager 注册广播接收器
        // 注意:本地广播只能动态注册
        mLocalBroadcastManager.registerReceiver(mDynamicBroadcastReceiver, mIntentFilter);

        Intent intent = new Intent("THIS_IS_A_DYNAMIC_BROADCAST");
        // 使用 LocalBroadcastManager 发送广播
        mLocalBroadcastManager.sendBroadcast(intent);
    }

    @Override
    protected void onDestroy() {

        ....

        unregisterReceiver(mDynamicBroadcastReceiver);
    }

    class DynamicBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("Guu", "receive dynamic broadcast!");
        }
    }

}

你可能感兴趣的:(Android)