Android——BroadcastReceiver的具体实现

BroadcastReceiver作为android的一个重要组件,主要是用来进行广播接收的。

android中的广播体现在各种方面,比如说开机,电量低,连接电源,解锁屏幕等等。这些action被广播出去后,app开发者只需要接收广播并进行处理即可

主要用到的一个类就是BroadcastReceiver,用的函数主要是重写onReceiver,下面介绍两种广播的使用方法:静态注册和动态注册

广播分为普通广播和有序广播

普通广播:正常的广播,所有Receiver都能接收,且不能阻止其他Receiver的接收,也没有能力结束广播

有序广播:按fliter的优先级进行广播(-1000到1000),从高到低广播,高的广播接收到之后传播到低优先级广播,完全可以终止广播的传递。

 

1. 静态注册

        <receiver android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="BROADCAST_ACTION" />
            </intent-filter>
        </receiver>

这个需要在manifest.xml文件中自己注册<intent-filter>来实现

package com.example.ld.broadcasttest;

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

public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = "MyReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Toast.makeText(context,intent.getStringExtra("msg"),Toast.LENGTH_SHORT).show();
    }
}

自己需要重新定义一个类,继承BrocastReceiver类

        sentBtn = (Button)findViewById(R.id.send);
        sentBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Log.d(TAG,"send a message to MY_Broadcast");
                Intent intent = new Intent();
                intent.setAction("BROADCAST_ACTION");
                //Intent intent  = new Intent("BROADCAST_ACTION");
                intent.putExtra("msg","hello myReceiver");
                sendBroadcast(intent);
                Log.d(TAG,"send success");

            }
        });

在Activity中发送广播即可,发送的方式为sendBroadcast(intent)

特点:静态注册的广播,即使activity死掉之后,他还是可以接收到广播消息的,可以用来设置开机启动等功能

2. 动态注册

package com.example.ld.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class SecondReceiver extends BroadcastReceiver {

    private static final String TAG="SecondReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        String msg = intent.getStringExtra("msg");
        Log.i(TAG, msg);
    }
}

动态注册不需要再manifest.xml文件中进行注册,不过通常需要重新写一个类。另外需要在activity中自己进行注册。同样别忘了需要在onDestroy()中进行销毁

    SecondReceiver sr = new SecondReceiver();
    IntentFilter filter = new IntentFilter();

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

        filter.addAction("TEST_ACTION");

        sentBtn2 = (Button)findViewById(R.id.send2);
        sentBtn2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Log.d(TAG,"send a message to MY_Broadcast");
                Intent intent = new Intent();
                intent.setAction("TEST_ACTION");
                intent.putExtra("msg","hello myReceiver");
                sendBroadcast(intent);
                Log.d(TAG,"send success");

            }
        });


    }

    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(sr);
    }

 

posted @ 2017-09-04 19:27  东木刀纹  阅读(244)  评论(0编辑  收藏  举报