在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver
活动(Activity) - 用于表现功能
服务(Service) - 相当于后台运行的 Activity
广播(Broadcast) - 用于发送广播
广播接收器(BroadcastReceiver) - 用于接收广播
Intent - 用于连接以上各个组件,并在其间传递消息
Android中的广播事件有两种,一种就是系统广播事件,比如:ACTION_BOOT_COMPLETED(系统启动完成后触发),ACTION_TIME_CHANGED(系统时间改变时触发),ACTION_BATTERY_LOW(电量低时触发)等等。
另外一种是我们自定义的广播事件。
注册BroadcastReceiver有两种方式:
一种方式是,静态的在AndroidManifest.xml中用<receiver>标签生命注册,并在标签内用<intent- filter>标签设置过滤器。
另一种方式是,动态的在代码中先定义并设置好一个 IntentFilter 对象,然后在需要注册的地方调Context.registerReceiver()方法,如果取消时就调用 Context.unregisterReceiver()方法。
如果用动态方式注册的BroadcastReceiver的Context对象被销毁时,BroadcastReceiver也就自动取消注册了。(特别注意,有些可能需要后台监听的,如短信消息)
发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。
接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog。
另外,若在使用sendBroadcast()的方法是指定了接收权限,则只有在AndroidManifest.xml中用<uses- permission>标签声明了拥有此权限的BroascastReceiver才会有可能接收到发送来的Broadcast。
同样,若在注册BroadcastReceiver时指定了可接收的Broadcast的权限,则只有在包内的AndroidManifest.xml中 用<uses-permission>标签声明了,拥有此权限的Context对象所发送的Broadcast才能被这个 BroadcastReceiver所接收。
广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。
通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等
MainActivity.java:
package com.demo.broadcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* @ClassName: MainActivity
* @Description: Broadcast测试demo
* @author chenzheng
* @date 2014-9-4 上午10:15:05
*/
public class MainActivity extends Activity implements OnClickListener{
private Button sendStaticBtn;
private Button sendDynamicBtn;
private static final String STATICACTION = "com.demo.broadcast.static";
private static final String DYNAMICACTION = "com.demo.broadcast.dynamic";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendStaticBtn = (Button) findViewById(R.id.send_static);
sendDynamicBtn = (Button) findViewById(R.id.send_dynamic);
sendStaticBtn.setOnClickListener(this);
sendDynamicBtn.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
Log.e("MainActivity", "注册广播事件");
// 注册自定义动态广播消息
IntentFilter filter_dynamic = new IntentFilter();
filter_dynamic.addAction(DYNAMICACTION);
registerReceiver(dynamicReceiver, filter_dynamic);
}
private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收自定义动态注册广播消息");
if(intent.getAction().equals(DYNAMICACTION)){
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_static:
Log.e("MainActivity", "发送自定义静态注册广播消息");
Intent intent1 = new Intent();
intent1.setAction(STATICACTION);
intent1.putExtra("msg", "接收静态注册广播成功!");
sendBroadcast(intent1);
break;
case R.id.send_dynamic:
Log.e("MainActivity", "发送自定义动态注册广播消息");
Intent intent2 = new Intent();
intent2.setAction(DYNAMICACTION);
intent2.putExtra("msg", "接收动态注册广播成功!");
sendBroadcast(intent2);
break;
default:
break;
}
}
}
StaticReceiver.java:
package com.demo.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* @ClassName: StaticReceiver
* @Description: 自定义静态注册广播消息接收器
* @author chenzheng
* @date 2014-9-4 上午11:09:53
*/
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收自定义动态注册广播消息");
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".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=".StaticReceiver" > <intent-filter> <action android:name="com.demo.broadcast.static" /> </intent-filter> </receiver> <!-- 注册系统静态广播接收器 --> <receiver android:name=".SystemReceiver" > <intent-filter> <action android:name="android.intent.action.BATTERY_LOW" /> </intent-filter> </receiver> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello" /> <Button android:id="@+id/send_static" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送自定义静态注册广播" /> <Button android:id="@+id/send_dynamic" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送自定义动态注册广播" /> </LinearLayout>