暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

android广播接收器 broadcast

陈兴强的BLOG 2016-06-30
223

broadcast receiver 特点

对外部事件进行过滤,只对感兴趣的事件做出响应。

通知方式:activity、serice、NotificationManager.

注册方式:动态注册、静态注册

一、创建方式:


二、静态注册:

  1.        <receiver android:name=".MyReceiver">

  2.            <intent-filter android:priority="11">

  3.                <action android:name="com.example.mx.myapplication.intent.action.MyReceiver" />

  4.            </intent-filter>

  5.        </receiver>

  1. //android:priority 优先级,数字越大优先级高,可以不加

三、动态注册:

  1.            //点击按钮动态注册

  2.            case R.id.btnReg:

  3.                Toast.makeText(this,"点击了注册",Toast.LENGTH_SHORT).show();

  4.                if(receiver==null){

  5.                    receiver=new MyReceiver();

  6.                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));


  7.                }

  8.                break;

四、屏蔽广播

  1.    public void onReceive(Context context, Intent intent) {

  2.        // TODO: This method is called when the BroadcastReceiver is receiving

  3.        // an Intent broadcast.

  4.       //System.out.println("接受到了消息消息的内容是:"+intent.getStringExtra("data"));

  5.        System.out.println("MyReceiver接收到了消息");

  6.        abortBroadcast();

  7.    }

 

五、后面加 abortBroadcast可以屏蔽广播

但是要修改一处代码,发送意图的方式由sendBroadcast改为sendOrderedBroadcast

  1.            case R.id.btnSendMsg:

  2.                Toast.makeText(this,"点击了发送",Toast.LENGTH_SHORT).show();

  3. //                Intent i=new Intent(this,MyReceiver.class);

  4.                Intent i=new Intent(MyReceiver.ACTION);

  5.                i.putExtra("data","jikexueyuan");

  6.                //sendBroadcast(i);

  7.      

  8.                sendOrderedBroadcast(i,null);

  9.                break;

下面是一个广播接收器的Demo(用于发送和接收信息)

发送短信MainActivity.java

  1. package com.example.mx.myapplication;


  2. import android.content.Intent;

  3. import android.content.IntentFilter;

  4. import android.support.v7.app.AppCompatActivity;

  5. import android.os.Bundle;

  6. import android.view.View;

  7. import android.widget.Toast;


  8. public class MainActivity extends AppCompatActivity implements View.OnClickListener {


  9.    @Override

  10.    protected void onCreate(Bundle savedInstanceState) {

  11.        super.onCreate(savedInstanceState);

  12.        setContentView(R.layout.activity_main);

  13.        findViewById(R.id.btnSendMsg).setOnClickListener(this);

  14.        findViewById(R.id.btnReg).setOnClickListener(this);

  15.        findViewById(R.id.btnUnreg).setOnClickListener(this);


  16.    }


  17.    @Override

  18.    public void onClick(View v) {

  19.        switch (v.getId()){

  20.            case R.id.btnSendMsg:

  21.                Toast.makeText(this,"点击了发送",Toast.LENGTH_SHORT).show();

  22. //                Intent i=new Intent(this,MyReceiver.class);

  23.                Intent i=new Intent(MyReceiver.ACTION);

  24.                i.putExtra("data","jikexueyuan");

  25.                //sendBroadcast(i);

  26.                sendOrderedBroadcast(i,null);

  27.                break;

  28.            //点击按钮动态注册

  29.            case R.id.btnReg:

  30.                Toast.makeText(this,"点击了注册",Toast.LENGTH_SHORT).show();

  31.                if(receiver==null){

  32.                    receiver=new MyReceiver();

  33.                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));


  34.                }

  35.                break;

  36.            case R.id.btnUnreg:

  37.                Toast.makeText(this,"点击了注销",Toast.LENGTH_SHORT).show();

  38.                if(receiver!=null){

  39.                    unregisterReceiver(receiver);

  40.                    receiver=null;

  41.                }

  42.                break;

  43.        }

  44.    }




  45.    private MyReceiver receiver=null;

  46. }

接收信息1 MyReceiver.java

  1. package com.example.mx.myapplication;


  2. import android.content.BroadcastReceiver;

  3. import android.content.Context;

  4. import android.content.Intent;

  5. import android.content.IntentFilter;


  6. public class MyReceiver extends BroadcastReceiver {

  7.    public static final String ACTION="com.example.mx.myapplication.intent.action.MyReceiver";


  8.    public MyReceiver() {

  9.    }


  10.    @Override

  11.    public void onReceive(Context context, Intent intent) {

  12.        // TODO: This method is called when the BroadcastReceiver is receiving

  13.        // an Intent broadcast.

  14.       //System.out.println("接受到了消息消息的内容是:"+intent.getStringExtra("data"));

  15.        System.out.println("MyReceiver接收到了消息");

  16.        abortBroadcast();

  17.    }

  18. }

接收信息2 MyReceiver.java

  1. package com.example.mx.myapplication;


  2. import android.content.BroadcastReceiver;

  3. import android.content.Context;

  4. import android.content.Intent;


  5. public class MyReceiver1 extends BroadcastReceiver {

  6.    public MyReceiver1() {

  7.    }


  8.    @Override

  9.    public void onReceive(Context context, Intent intent) {

  10.        // TODO: This method is called when the BroadcastReceiver is receiving

  11.        // an Intent broadcast.

  12.        System.out.println("MyReceiver 1 接收到了消息");


  13.    }

  14. }

布局界面 activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout

  3.    xmlns:android="http://schemas.android.com/apk/res/android"

  4.    xmlns:tools="http://schemas.android.com/tools"

  5.    android:layout_width="match_parent"

  6.    android:layout_height="match_parent"

  7.    android:orientation="vertical">


  8.    <Button

  9.        android:id="@+id/btnSendMsg"

  10.        android:text="发送消息"

  11.        android:layout_width="wrap_content"

  12.        android:layout_height="wrap_content" />

  13.    <Button

  14.        android:layout_width="wrap_content"

  15.        android:layout_height="wrap_content"

  16.        android:text="注册接收器"

  17.        android:id="@+id/btnReg"

  18.        />

  19.    <Button

  20.        android:layout_width="wrap_content"

  21.        android:layout_height="wrap_content"

  22.        android:text="注销接收器"

  23.        android:id="@+id/btnUnreg"/>

  24. </LinearLayout>

配制界面:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3.    package="com.example.mx.myapplication">


  4.    <application

  5.        android:allowBackup="true"

  6.        android:icon="@mipmap/ic_launcher"

  7.        android:label="@string/app_name"

  8.        android:supportsRtl="true"

  9.        android:theme="@style/AppTheme">

  10.        <activity android:name=".MainActivity">

  11.            <intent-filter>

  12.                <action android:name="android.intent.action.MAIN" />


  13.                <category android:name="android.intent.category.LAUNCHER" />

  14.            </intent-filter>

  15.        </activity>


  16.        <receiver android:name=".MyReceiver">

  17.            <intent-filter android:priority="11">

  18.                <action android:name="com.example.mx.myapplication.intent.action.MyReceiver" />

  19.            </intent-filter>

  20.        </receiver>

  21.        <receiver

  22.            android:name=".MyReceiver1"

  23.            android:enabled="true"

  24.            android:exported="true">

  25.            <intent-filter android:priority="10">

  26.                <action android:name="com.example.mx.myapplication.intent.action.MyReceiver"></action>

  27.            </intent-filter>

  28.        </receiver>


  29.    </application>


  30. </manifest>



文章转载自陈兴强的BLOG,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论