- 浏览: 294659 次
- 性别:
- 来自: 上海
最新评论
-
再_见孙悟空:
写的不错
Dialog -
a549262189:
支持下,正好在学习这块的内容!
Android Gesture Detector -
dcsff:
受教了
LayoutInflater -
庆_啊:
真厉害。
LayoutInflater -
ghj234:
学习了,哈哈
LayoutInflater
Broadcast Receiver用于接收并处理广播通知(broadcast announcements)。多数的广播是系统发起的,如地域变换、电量不足、来电来信等。程序也可以播放一个广播。程序可以有任意数量的 broadcast receivers来响应它觉得重要的通知。broadcast receiver可以通过多种方式通知用户:启动activity、使用NotificationManager、开启背景灯、振动设备、播放声音等,最 典型的是在状态栏显示一个图标,这样用户就可以点它打开看通知内容。
通常我们的某个应用或系统本身在某些事件(电池电量不足、来电来短信)来临时会广播一个Intent出去,我们可以利用注册一个Broadcast Receiver来监听到这些Intent并获取Intent中的数据。
1. 我们自己的程序主动广播Intent
final String BROADCAST = "com.forrest.action.mybroadcast"; Intent intent = new Intent(BROADCAST); // 对应setAction() intent.putExtra("data_title", "来短信啦"); intent.putExtra("data_text", "美女你好,晚上可有空"); sendBroadcast(intent);
2.接收广播
接收broadcast需要注册一个Broadcast Receiver,并且要注册一个Intent Filter来制定Broadcase Receiver是对哪些Intent进行监听。
(1)注册Broadcast Receiver
一个Broadcast receiver只有一个简单的回调函数:onReceive(Context curContext, Intent broadcastMsg),当一个广播消息被Receiver监听到时,Android会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则android会弹出超时dialog。 此时是否另开一个线程来处理耗时的操作呢?
Receiver只在onReceive方法执行时是激活状态,只要onReceive一返回,Receiver就不再是激活状态了。Receiver进程是被一个激活状态的broadcast receiver所保护而不被系统终止的,一旦onReceive返回,Receiver进程broadcast receiver所保护而变为一个空进程,空进程是可以在任意时刻被终止的。这就带来了一个问题:当响应一个广播信息的处理十分耗时的时候,那么就应该把这个处理放在一个单独的线程里去执行,来保证主线程里的其他用户交互组件能够继续运行,而一旦这么做,当onReceive()唤起一个线程后就会马上返回,这时就会把Receiver进程放到被终止的境地。解决这个问题的方案是在onReceive()里开始一个Service,让这个Service去做这件事情,那么系统就会认为这个进程里还有活动正在进行。
(2)注册/注销Broadcast Receiver
1)在AndroidManifest.xml中注册
<receiver android:name="Receiver1"> <intent-filter> <!-- 和Intent中的action对应 --> <action android:name="com.forrest.action.mybroadcast"/> </intent-filter> </receiver>
2)在代码中注册
IntentFilter filter = new IntentFilter("com.forrest.action.mybroadcast"); // 和广播中Intent的action对应 MyBroadcastReceiver br = new MyBroadcastReceiver(); registerReceiver(new MyBroadcastReceiver(), filter);
3)注销
unregisterReceiver(br);
3. 示例代码
public class Receiver1 extends BroadcastReceiver { private Context context; public static final int NOTIFICATION_ID = 10001; public void onReceive(Context context, Intent intent) { this.context = context; showNotification(); } private void showNotification() { Notification notification = new Notification(R.drawable.icon, "来电话啦...", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); notification.setLatestEventInfo(context, "来电话啦...嘿嘿", "赶紧接电话,否则误大事了", contentIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService( android.content.Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); } }
public class Receiver2 extends BroadcastReceiver { private Context context; @Override public void onReceive(Context context, Intent intent) { this.context = context; deleteNotification(); } private void deleteNotification() { NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); notificationManager.cancel(Receiver1.NOTIFICATION_ID); } }
public class MainActivity extends Activity { private final String ACTION_SEND = "com.forrest.action.SENDMESSAGE", ACTION_CLEAR = "com.forrest.action.CLEARNOTIFICATION"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ( (Button) findViewById(R.id.btn1) ).setOnClickListener(new OnClickListener() { public void onClick(View v) { clickMenuItem(ACTION_SEND); } }); ( (Button) findViewById(R.id.btn2) ).setOnClickListener(new OnClickListener() { public void onClick(View v) { clickMenuItem(ACTION_CLEAR); } }); } private void clickMenuItem(final String action) { Intent intent = new Intent(action); sendBroadcast(intent); } }
注册Broadcast Reciver
<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> <receiver android:name="Receiver1"> <intent-filter> <action android:name="com.forrest.action.SENDMESSAGE"/> </intent-filter> </receiver> <receiver android:name="Receiver2"> <intent-filter> <action android:name="com.forrest.action.CLEARNOTIFICATION"/> </intent-filter> </receiver> </application>
4.
评论
后来同事用启动service解决了。
在你这里我找到原因。多谢。
<action android:name="com.forrest.action.SENDMESSAGE"/>
这个里面的名字是自己取的,还是系统提供的啊?
发表评论
-
如果在Eclipse中debug
2010-05-02 14:15 0最基本的操作是: 1, 首先在一个java文件中设断点,然后 ... -
How to download sourcecode of android with cygwin
2010-04-14 16:07 5081cygwin是一个在windows平台 ... -
TelephoneManager
2010-03-08 16:04 01. protected void onCreate(Bun ... -
eclipse c
2010-03-07 21:05 0Eclipse 除 了可以开发Java之外,还支持了许 ... -
android sdk安装
2010-02-02 23:37 34491.下载:http://developer.android.c ... -
google map my demo
2010-02-02 23:36 3163<uses-library android:name=& ... -
Toast
2010-02-02 23:35 14231. (1) public class ToastActi ... -
如何在MapView上画图
2010-02-02 23:34 1561http://ophone8.com/thread-4340- ... -
Service
2010-02-02 23:33 31141. Service生命周期 (1)Servic ... -
Notification
2010-02-02 23:33 18851. Notification构造器的参数 分别为: ... -
导航相关概念
2010-02-02 23:32 16731. Google Map的定位 (1)Goog ... -
PendingIntent和Intent
2010-02-02 23:28 9490Notification n = new Notificati ... -
Broadcast调用Service做的一个定时器
2010-02-02 23:28 36721. public class AlarmActivity ... -
LayoutInflater
2010-02-02 23:27 21764一般来讲,我们用LayoutInflater做一件事:infl ... -
Android's Components 生命周期
2010-02-02 23:27 2110Android中,Component(activity、ser ... -
Activity文档翻译
2010-02-02 23:26 1540转自:http://www.blogjava.net/m ... -
Intent
2010-01-20 10:51 011 public class MumActivity ex ... -
aidl
2010-01-05 11:24 30271. android进程之间如何通讯呢(在不同的进程中怎样传递 ... -
Gallery 画廊
2009-12-17 20:25 33291. images.xml <?xml ver ... -
Intent中的Flag参数
2009-12-16 18:12 6440Intent i = new Intent(B.this, C ...
相关推荐
利用广播Broadcast Receiver,在2个不同的Activity传递数据
四大组件 Activity Service content provider broadcast receiver
Android新手Broadcast Receiver简单实现 发送广播并实现接收广播
本示例 利用Broadcast Receiver 启动Service,在这里同时又启动了一个Activity用于提示服务启动成功
广播接受者(Broadcast Receiver)的简介与使用
一个关于Android四大组件Broadcast Receiver的小demo
该文档是简单的介绍了其android中的广播机制的用法,比较通俗易懂。
文章目录Android四大组件之Broadcast Receiver概述广播接收器(Broadcast Receiver)Broadcast Receiver简介Broadcast Receiver的注册一. 静态注册二. 动态注册三. 本地广播发送Broadcast Receiver广播的三种发送...
android-simple-broadcast-receiver-app 简单的Android Broadcast Receiver使用示例
下面就具体介绍一下Broadcast Receiver组件的用法。 下面是Android Doc中关于BroadcastReceiver的概述: ①广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,...
本文实例讲述了Android开发中BroadcastReceiver用法。分享给大家供大家参考。具体分析如下: 在Android系统中,广播(Broadcast)是在...我们同样可以在自己的应用程序中实现Broadcast Receiver来监听和响应广播的Inten
首先创建需要用到的控件,然后绑定控件,再设置监听器,定义一个MusicService 类,创建broadcast receiver,intentfilter 和Mediaplayer在musics receive里面发送广播,将会在Activity组件中的broadcast receive接收...
Android自学笔记-17-广播接收者(BroadcastReceiver)
当用户收到X中的写入,广播,通话请求或关机通知时,此程序会弹出一个窗口。有点像kwrited,但不需要(或关心)kde支持。
短信接收器 传入SMS广播接收器-Android示例在此android应用示例中,我们显示了何时广播任何新的SMS事件(当收到新的SMS时)SMS数据。 接收短信到Android广播接收器。 可以从SMS Recibido接收电子邮件。
三种应用程序基本组件——activity, service和broadcast receiver——是使用称为intent的消息来激活的。Intent消息传递是一种组件间运行时绑定的机制. intent是Intent对象, 它包含了需要做的操作的描述, 或者, ...
图像上载-BroadCastReceiver 正在监听用户使用相机(照片)的所有事件的应用程序。 用户捕获照片后,该照片即被上传到服务器。后端在后端文件夹中接收图像的服务器的代码。
使用人群:BroadcastReceiver初学者。 里面涉及BroadcastReceiver的静态注册(无序广播)、动态注册(无序广播)、有序广播。 项目对应:https://shuaici.blog.csdn.net/article/details/118337894
into apps that don't provide an open API, for example if the app doesn't have a broadcast receiver and it doesn't accept data when an activity loads. As long as this keyboard is active and a text box ...