广播接收器(Broadcast Receiver)

Android中的广播主要可以分为:标准广播和有序广播

一.标准广播

1.标准广播特点:

<1>同级别接收先后是随机的(无序)

<2>级别低的后收到广播(优先级别在intent-filter中的priority中声明,-1000到1000之间,值越大,优先级越高)

<3>接收器不能截断广播的继续传播,也不能处理广播

<4>同级别动态注册高于静态注册(在代码中注册为动态注册,在           AndroidManifest.xml中注册为静态注册)

2.发送标准广播实例

public class MainActivity extends AppCompatActivity {
    private Button mSendButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSendButton = (Button) findViewById(R.id.send_btn);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("BC");
                intent.putExtra("msg","标准广播");
                sendBroadcast(intent);
            }
        });
    }
}
public class MyBroadcasts extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s= intent.getStringExtra("msg");
        Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
    }
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.lance.broadcastdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".MyBroadcasts">
            <intent-filter>
                <action android:name="BC"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

二.有序广播:

1.有序广播特点

<1>同级别接收顺序是随机的

<2>能截断广播的继续传播,高级别的广播接收器收到广播后,可以决定把该广播           是否截断

<3>接收器能截断广播的继续传播,也能处理广播

<4>同级别动态注册高于静态注册

2.有序广播实例

 

public class MainActivity extends AppCompatActivity {
    private Button mSendButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSendButton = (Button) findViewById(R.id.send_btn);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("BC");
                intent.putExtra("msg","广播");
                sendOrderedBroadcast(intent,null);
            }
        });
    }
}
public class MyBroadcasts extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s= intent.getStringExtra("msg");
        Toast.makeText(context,"普通:"+s,Toast.LENGTH_SHORT).show();
    }
}
public class AnotherBroadcasts extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String s= intent.getStringExtra("msg");
        Toast.makeText(context,"有序:"+s,Toast.LENGTH_SHORT).show();
        abortBroadcast();//表示将这条广播截断,后面的广播接收器无法再接收。
    }
}
<receiver android:name=".MyBroadcasts">
    <intent-filter>
        <action android:name="BC"/>
    </intent-filter>
</receiver>
<receiver android:name=".AnotherBroadcasts">
    <intent-filter android:priority="100">
        <action android:name="BC"/>
    </intent-filter>
</receiver>

这是有序广播,在AnotherBroadcasts的android:priority属性设置了优先级,优先级高的先接收广播,所以在Mybroadcasts之前收到广播,由于在AnotherBroadcasts中设置了截断,所以后面的无法再接收这条广播,Mybroadcasts无法弹出Toast 信息。

 来自个人博客:广播接收器(Broadcast Receiver)

posted on 2017-03-14 21:49  懂你在爱我  阅读(573)  评论(0)    收藏  举报

导航