2012年12月6日 星期四
廣播機制 Broadcast 和BroadReceiver
2011-03-23 16:11:45| 分类: Android | 标签:android intent context import private |字号 订阅
1、作用
异步操作、通知
2、两种注册方式
2.1 在AndroidManifest.xml中注册
<receiver android:name="EoeAndroidReceiver">
<intent-filter>
<action android:name="com.eoeandroid.action.NEW_BROADCAST" />
</intent-filter>
</receiver>
2.2 在代码中直接注册
IntentFilter filter = new IntentFilter(NEW_BROADCAST);
EoeAndroidReceiver eoeandroidReceiver = new EoeAndroidReceiver();
registerReceiver(eoeandroidReceiver,filter);
3、注销
unregisterReceiver(eoeandroidReceiver);
4、发送广播
sendBroadcast(intent);
5、接收广播
onReceiver(Context context,Intent intent);
6、示例
6.1 MainActivity.Java
package com.browan.server;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private static final int ITEM0 = Menu.FIRST;
private static final int ITEM1 = Menu.FIRST + 1;
private static final String ACTION_1 = "com.eoeandroid.action.NEW_BROADCAST1";
private static final String ACTION_2 = "com.eoeandroid.action.NEW_BROADCAST2";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//相关布局模板文件main.xml
setTitle("按menu菜单触发测试");
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0,ITEM0,0,"显示Notification");
menu.add(0,ITEM1,0,"消除Notification");
menu.findItem(ITEM1);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case ITEM0:
actionClickMenuItem1();
break;
case ITEM1:
actionClickMenuItem2();
break;
}
return true;
}
private void actionClickMenuItem1() {
// TODO Auto-generated method stub
Intent intent = new Intent(ACTION_1);
sendBroadcast(intent);
}
private void actionClickMenuItem2() {
// TODO Auto-generated method stub
Intent intent = new Intent(ACTION_2);
sendBroadcast(intent);
}
}
6.2 EoeAndroidReceiver1.Java
package com.browan.server;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class EoeAndroidReceiver1 extends BroadcastReceiver{
private static final int NOTIPICATION_ID = 21321;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context = context;
showNotification();
}
@SuppressWarnings("unused")
private void showNotification() {
// TODO Auto-generated method stub
// 实例化通知管理器
NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 实例化一个通知,并指定其图标和标题(在提示栏上显示)
Notification notification = new Notification(R.drawable.icon,"在EoeAndroidReceiver1里",System.currentTimeMillis());
// 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView)
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context,MainActivity.class), 0);
// 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示)
notification.setLatestEventInfo(context, "通知发送人", "我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent);
// 发出通知(其中第一个参数为通知标识符)
notificationManager.notify(getNotipicationId(), notification);
}
public static int getNotipicationId() {
return NOTIPICATION_ID;
}
}
6.3 EoeAndroidReceiver2.Java
package com.browan.server;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class EoeAndroidReceiver2 extends BroadcastReceiver{
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context = context;
deleteNotification();
}
private void deleteNotification() {
// TODO Auto-generated method stub
NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(EoeAndroidReceiver1.getNotipicationId());
}
}
6.4 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.browan.server"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.NEW_BROADCAST1" />
<uses-permission android:name="android.permission.NEW_BROADCAST2" />
<application android:icon="@drawable/icon" 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>
<!--注册两个BroadcastReceiver -->
<receiver android:name="EoeAndroidReceiver1">
<intent-filter>
<action android:name="com.eoeandroid.action.NEW_BROADCAST1" />
</intent-filter>
</receiver>
<receiver android:name="EoeAndroidReceiver2">
<intent-filter>
<action android:name="com.eoeandroid.action.NEW_BROADCAST2" />
</intent-filter>
</receiver>
</application>
</manifest>
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言