Android菜鸟的成长笔记(26)——普通广播与有序广播

BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程。

我们来写一个最简单的广播接收过程

先在manifest中定义一个广播接受者

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcasttest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver 
            android:name="com.example.broadcasttest.MyBroadCast">
            <intent-filter >
                <action android:name="com.meritit.action.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application>
广播接收者

public class MyBroadCast extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
	}

}
向广播接收者发送广播

package com.example.broadcasttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setAction("com.meritit.action.MY_BROADCAST");
				intent.putExtra("msg", "阳光小强");
				sendBroadcast(intent);
			}
		});
	}
}
运行结果:

Android菜鸟的成长笔记(26)——普通广播与有序广播

上面的例子是一个普通的广播接受者,下面我们来修改一下代码看看有序的广播接受者

        <receiver 
            android:name="com.example.broadcasttest.MyBroadCast">
            <intent-filter android:priority="20">
                <action android:name="com.meritit.action.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
        <receiver 
            android:name="com.example.broadcasttest.MyBroadCast2">
            <intent-filter android:priority="0" >
                <action android:name="com.meritit.action.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
两个广播接收者设置了优先级,上面的优先级比下面的高

Intent intent = new Intent();
intent.setAction("com.meritit.action.MY_BROADCAST");
intent.putExtra("msg", "阳光小强");
sendOrderedBroadcast(intent, null);
发送有序广播,注意是sendOrderedBroadcast

public class MyBroadCast extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
		Bundle bundle = new Bundle();
		bundle.putString("msg2", "有序的广播");
		setResultExtras(bundle);
		
		//如果不想继续传播
		//abortBroadcast();
	}

}
优先级高的MyBroadCast先接收到,有序广播接收者可以添加新数据给下个等级的接受者。这种形式就有点像拦截器。

public class MyBroadCast2 extends BroadcastReceiver{

	private static final String TAG = "broadcast";

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = getResultExtras(true);
		String msg = intent.getStringExtra("msg");
		String msg2 = bundle.getString("msg2");
		Log.i(TAG, msg);
		Log.i(TAG, msg2);
	}

}

最后输出结果:

Android菜鸟的成长笔记(26)——普通广播与有序广播
除了接收用户发送的广播之外,BroadcastReceiver还有一个重要作用,就是接收系统广播。

详细请看:http://developer.android.com/reference/android/content/Intent.html

下面列出Android常见的广播。

例如检测电池电量过低

        <receiver 
            android:name="com.example.broadcasttest.MyBroadCast3">
            <intent-filter >
                <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
            </intent-filter>
        </receiver>

public class MyBroadCast3 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		int current = bundle.getInt("level");
		int total = bundle.getInt("scale");
		if(current * 1.0 / total < 0.15){
			Toast.makeText(context, "电池电量过低,请尽快充电", Toast.LENGTH_LONG).show();
		}
	}

}







Android菜鸟的成长笔记(26)——普通广播与有序广播,布布扣,bubuko.com

Android菜鸟的成长笔记(26)——普通广播与有序广播

上一篇:android:The application may be doing too much work on its main thread.


下一篇:Android中 View not attached to window manager错误的解决办法