«

Android广播接收器Broadcast Receiver-android学习之旅(十二)

时间:2024-3-2 17:37     作者:韩俊     分类: Android


首先继承BroadcastReceiver类,并在manifest中注册

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");

    }
}

在mainifest中注册

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>

动态注册和取消广播接收器

上代码:
Receiver部分:

public class MyReceiver extends BroadcastReceiver {
    public static final String ACTION = "peng.liu.testview.intent.action.MyReceiver";
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getStringExtra("data")+"hello");
    }
}

主类部分:

public class MainActivity extends Activity implements View.OnClickListener{
    private MyReceiver receiver = null;
    private Button send,reg,unReg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.send).setOnClickListener(this);
        findViewById(R.id.reg).setOnClickListener(this);
        findViewById(R.id.unReg).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send:
                Intent intent = new Intent(MyReceiver.ACTION);
                intent.putExtra("data","jiekxueyuan");
                sendBroadcast(intent);
                break;
            case R.id.reg:
                if (receiver == null){
                    receiver = new MyReceiver();
                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
                }
                break;
            case R.id.unReg:
                if (receiver != null){
                    unregisterReceiver(receiver);
                    receiver = null;
                }
                break;

        }
    }
}

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reg"
        android:id="@+id/reg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unReg"
        android:id="@+id/unReg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        android:id="@+id/send" />
</LinearLayout>

广播的优先级

这次我们在manifest中静态注册

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="8">
                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="9">
                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>
            </intent-filter>
        </receiver>

android:priority:用于设置优先级,数字越大,优先级越高。

高优先级的终端广播

//发送部分注意是发送sendOrderedBroadcast(intent,null);
Intent intent = new Intent(MyReceiver.ACTION);
                intent.putExtra("data","jiekxueyuan");
                sendOrderedBroadcast(intent,null);

接收部分

public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getStringExtra("data"));
        //这一户用于中断后面的低优先级的接受
        abortBroadcast();
    }
}

标签: android

热门推荐