本文实例讲述了Android开发中BroadcastReceiver用法。分享给大家供大家参考。具体分析如下:
在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制。
Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的。我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现Broadcast Receiver来监听和响应广播的Intent。
事件的广播通过创建Intent对象并调用sendBroadcast()方法将广播发出。事件的接受是通过定义一个继承BroadcastReceiver的类来实现的,继承该类后覆盖其onReceive()方法,在该方法中响应事件。
下面是android系统中定义了很多标准的Broadcast Action来响应系统的广播事件。
①ACTION_TIME_CHANGED(时间改变时触发)
②ACTION_BOOT_COMPLETED(系统启动完成后触发)--比如有些程序开机后启动就是用这种方式来实现的
③ACTION_PACKAGE_ADDED(添加包时触发)
④ACTION_BATTERY_CHANGED(电量低时触发)
下面看一个例子:
我们在一个按钮上绑定一个事件,事件通过发送一个广播来触发logcat打出一个log。
先看manifest文件。
<?xml version=\"1.0\" encoding=\"utf-8\"?> <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.example.test\" android:versionCode=\"1\" android:versionName=\"1.0\" > <uses-sdk android:minSdkVersion=\"8\" android:targetSdkVersion=\"16\" /> <application android:allowBackup=\"true\" android:icon=\"@drawable/ic_launcher\" android:label=\"@string/app_name\" android:theme=\"@style/AppTheme\" > <activity android:name=\"com.example.broadcast.BroadcastReceiverActivity\" android:label=\"@string/app_name_bc\" > <intent-filter> <action android:name=\"android.intent.action.MAIN\" > </action> <category android:name=\"android.intent.category.LAUNCHER\" > </category> </intent-filter> </activity> <receiver android:name=\"com.example.broadcast.HelloBroadReciever\" > <intent-filter> <action android:name=\"comz.test.printlog\" > </action> </intent-filter> </receiver> </application> </manifest>
上面声明了一个receiver。接收名字是comz.test.printlog的消息。
看activity:
package com.example.broadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.example.test.R; public class BroadcastReceiverActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b1 = (Button) findViewById(R.id.broadcastBtn); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(\"mason\", \"here\"); // 定义一个intent Intent intent = new Intent().setAction(\"comz.test.printlog\") .putExtra(\"info\", \"here is your info.\"); // 广播出去 sendBroadcast(intent); } }); } }
在这段代码中,定义一个intent并发送广播出去。
看BroadReceiver的代码:
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class HelloBroadReciever extends BroadcastReceiver { // 如果接收的事件发生 @Override public void onReceive(Context context, Intent intent) { Log.e(\"mason\", \"on receive\"); if (intent.getAction().equals(\"comz.test.printlog\")) { Log.e(\"mason\", intent.getStringExtra(\"info\")); } } }
这是BroadcastReceiver的代码。
在接收到消息之后,如果消息是comz.test.printlog,则打印消息。
希望本文所述对大家的Android程序设计有所帮助。
本文地址:https://www.stayed.cn/item/23511
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我