broadcast receiverreceiver怎么启动

【android】使用BroadcastReceiver实现开机启动Service或Activity
我的图书馆
【android】使用BroadcastReceiver实现开机启动Service或Activity
【android】使用BroadcastReceiver实现开机启动Service或Activity
发帖日期: 16:01:25
首先必须有一个BroadcastReceiver以便监听手机开机intent, 而该receiver又负责启动你的service或者activity.
public class yourReceiver extends BroadcastReceiver
public&void onReceive(Context context, Intent intent)
Intent&i = new Intent(yourService.ACTION_START);
i.setClass(context, yourService.class);
context.startService(i);
其次,就需要在Manifest文件中声明一下intent-filter: 先加入使用权限声明:
&uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/&
然后加入receiver 注册声明:
&receiver android:name=".yourReceiver" &
&& &intent-filter&
&& & &&action android:name="android.intent.action.BOOT_COMPLETED" /&
& &/intent-filter&
&/receiver&
还是Android好, 能够监听开机intent, j2me中一直无法实现开机自启动,通过Push Registry来实现自启动也好麻烦。 不知道Android中的这个user-permission是只需要这么声明就行了么。
Symbain 的开机自启动没有研究,不过比较麻烦,还区分第几版之类的东西。
这样开机后yourReceiver 就能收到开机的intent,然后自动启动你的service 或activity
发表评论:
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&emily_ad 的BLOG
用户名:emily_ad
文章数:24
访问量:64661
注册日期:
阅读量:5863
阅读量:12276
阅读量:383104
阅读量:1074664
51CTO推荐博文
一、BroadcastReceiver的简介
用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似.
广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码.如:时区改变的通知,电池电量不足、用户改变了语言偏好或者开机启动等.
广播接收器没有用户界面,但是,它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.
二、BroadcastReceiver的两种注册方式
1)静态注册
静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action
首先,新建一个Android项目---&取名BroadcastReceiverDemo01
BroadcastActivity.java的代码
public&class&BroadcastActivity&extends&Activity&{&&&&&&&&&&private&Button&send&=&null;&&&&&&&&&&@Override&&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&&&&&&&&super.onCreate(savedInstanceState);&&&&&&&&&setContentView(R.layout.main);&&&&&&&&&send&=&(Button)findViewById(R.id.sned);&&&&&&&&&send.setOnClickListener(new&BroadcastListener());&&&&&}&&&&&&&&&&class&BroadcastListener&implements&OnClickListener{&&&&&&&&&&@Override&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&&&&&&&&&&&&&&System.out.println(&------------&);&&&&&&&&&&&&&Intent&intent&=&new&Intent();&&&&&&&&&&&&&intent.setAction(Intent.ACTION_EDIT);&&&&&&&&&&&&&BroadcastActivity.this.sendBroadcast(intent);&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&&}&
TestReceiver.java的代码
public&class&TestReceiver&extends&BroadcastReceiver&{&&&&&&&&&&public&TestReceiver()&{&&&&&&&&&System.out.println(&TestReceiver&create......&);&&&&&}&&&&&&@Override&&&&&public&void&onReceive(Context&arg0,&Intent&arg1)&{&&&&&&&&&System.out.println(&receive......&);&&&&&}&&}&
main.xml的布局文件
&?xml&version=&1.0&&encoding=&utf-8&?&&&LinearLayout&xmlns:android=&/apk/res/android&&&&&&android:orientation=&vertical&&&&&&android:layout_width=&fill_parent&&&&&&android:layout_height=&fill_parent&&&&&&&&&TextView&&&&&&&android:layout_width=&fill_parent&&&&&&&android:layout_height=&wrap_content&&&&&&&android:text=&@string/hello&&&&&&/&&&&&&&&&&&&Button&&&&&&&&&android:id=&@+id/sned&&&&&&&&&&android:layout_width=&wrap_content&&&&&&&&&&&android:layout_height=&wrap_content&&&&&&&&&&&android:text=&@string/send&&&&&&/&&&/LinearLayout&&
strings.xml
&?xml&version=&1.0&&encoding=&utf-8&?&&&resources&&&&&&&string&name=&hello&&Hello&World,&TestReceiver!&/string&&&&&&&string&name=&app_name&&broadcast&/string&&&&&&&string&name=&send&&发送&/string&&&/resources&&
AndroidManifest.xml
&?xml&version=&1.0&&encoding=&utf-8&?&&&manifest&xmlns:android=&/apk/res/android&&&&&&&&package=&com.gem.activity&&&&&&&&android:versionCode=&1&&&&&&&&android:versionName=&1.0&&&&&&&&application&android:icon=&@drawable/icon&&android:label=&@string/app_name&&&&&&&&&&&&activity&android:name=&.BroadcastActivity&&&&&&&&&&&&&&&&&&&&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&--&&&&&&&&&&&receiver&android:name=&.TestReceiver&&&&&&&&&&&&&&&&intent-filter&&&&&&&&&&&&&&&&&&&action&android:name=&android.intent.action.EDIT&&&/action&&&&&&&&&&&&&&&/intent-filter&&&&&&&&&&&/receiver&&&&&&&/application&&&&&&&uses-sdk&android:minSdkVersion=&8&&/&&&&/manifest&&&
当用户点击发送的时候,程序会调用onReceive()方法
2)动态注册
动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action.
首先,新建一个Android项目---&取名BroadcastReceiverDemo02
BroadcastActivity.java的代码
public&class&BroadcastActivity&extends&Activity&{&&&&&private&Button&&&&&&private&Button&registerR&&&&&private&Button&unregisterR&&&&&private&MyReceiver&myR&&&&&&&&&&@Override&&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&&&&&&&&super.onCreate(savedInstanceState);&&&&&&&&&setContentView(R.layout.main);&&&&&&&&&&&&&&&&&&send&=&(Button)findViewById(R.id.send);&&&&&&&&&&&&&&&&&send.setOnClickListener(new&SendButtontListener());&&&&&&&&&&&&&&&&&&registerReceiver&=&(Button)findViewById(R.id.registerReceiver);&&&&&&&&&registerReceiver.setOnClickListener(new&RegisterReceiverButtonListener());&&&&&&&&&&&&&&&&&&unregisterReceiver&=&(Button)findViewById(R.id.unregisterReceiver);&&&&&&&&&unregisterReceiver.setOnClickListener(new&UnregisterReceiverButtonListener());&&&&&&&&&&&&&&}&&&&&&&&&&class&SendButtontListener&implements&OnClickListener{&&&&&&&&&&@Override&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&Intent&intent&=&new&Intent();&&&&&&&&&&&&&intent.setAction(&Intent.ACTION_EDIT&);&&&&&&&&&&&&&BroadcastActivity.this.sendBroadcast(intent);&&&&&&&&&&&&&System.out.println(&send-----&);&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&class&RegisterReceiverButtonListener&implements&OnClickListener{&&&&&&&&&&&&&&&&&&@Override&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&myReceiver&=&new&MyReceiver();&&&&&&&&&&&&&IntentFilter&filter&=&new&IntentFilter();&&&&&&&&&&&&&filter.addAction(&Intent.ACTION_EDIT&);&&&&&&&&&&&&&&&&&&&&&&&&&&registerReceiver(myReceiver,&filter);&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&class&UnregisterReceiverButtonListener&implements&OnClickListener{&&&&&&&&&@Override&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&&&&&&&&&&&&&&unregisterReceiver(myReceiver);&&&&&&&&&&&&&&System.out.println(&close-----&);&&&&&&&&&}&&&&&}&&&&&&&&&&&}&
MyReceiver.java的代码
public&class&MyReceiver&extends&BroadcastReceiver&{&&&&&&@Override&&&&&public&void&onReceive(Context&context,&Intent&intent)&{&&&&&&&&&&&&&&System.out.println(&onReceive......&);&&&&&}&&}&
main.xml的布局文件
&?xml&version=&1.0&&encoding=&utf-8&?&&&LinearLayout&xmlns:android=&/apk/res/android&&&&&&android:orientation=&vertical&&&&&&android:layout_width=&fill_parent&&&&&&android:layout_height=&fill_parent&&&&&&&&&TextView&&&&&&&android:layout_width=&fill_parent&&&&&&&android:layout_height=&wrap_content&&&&&&&android:text=&@string/hello&&&&&&/&&&&&&&&&&&&Button&&&&&&&&&android:id=&@+id/send&&&&&&&&&&android:layout_width=&wrap_content&&&&&&&&&&&android:layout_height=&wrap_content&&&&&&&&&&&android:text=&@string/send&&&&&&/&&&&&&&Button&&&&&&&&&android:id=&@+id/registerReceiver&&&&&&&&&&android:layout_width=&wrap_content&&&&&&&&&&&android:layout_height=&wrap_content&&&&&&&&&&&android:text=&@string/registerReceiver&&&&&&/&&&&&&&Button&&&&&&&&&android:id=&@+id/unregisterReceiver&&&&&&&&&&android:layout_width=&wrap_content&&&&&&&&&&&android:layout_height=&wrap_content&&&&&&&&&&&android:text=&@string/unregisterReceiver&&&&&&/&&&/LinearLayout&&
strings.xml
&?xml&version=&1.0&&encoding=&utf-8&?&&&resources&&&&&&&string&name=&hello&&Hello&World,&TestReceiver!&/string&&&&&&&string&name=&app_name&&broadcast&/string&&&&&&&string&name=&send&&发送广播&/string&&&&&&&string&name=&registerReceiver&&注册广播接收器&/string&&&&&&&string&name=&unregisterReceiver&&注销广播接收器&/string&&&/resources&&
AndroidManifest.xml
&?xml&version=&1.0&&encoding=&utf-8&?&&&manifest&xmlns:android=&/apk/res/android&&&&&&&&package=&com.gem.activity&&&&&&&&android:versionCode=&1&&&&&&&&android:versionName=&1.0&&&&&&&&application&android:icon=&@drawable/icon&&android:label=&@string/app_name&&&&&&&&&&&&activity&android:name=&.BroadcastActivity&&&&&&&&&&&&&&&&&&&&android:label=&@string/app_name&&&&&&&&&&&&&&&&intent-filter&&&&&&&&&&&&&&&&&&&action&android:name=&android.intent.action.MAIN&&/&&&&&&&&&&&&&&&&&&&category&android:name=&android.intent.category.LAUNCHER&&/&&&&&&&&&&&&&&&/intent-filter&&&&&&&&&&&/activity&&&&&&&&&&&&&&&&/application&&&&&&&uses-sdk&android:minSdkVersion=&8&&/&&&&/manifest&&
当用户点击发送的时候,程序没有注册BroadcastReceiver,当用户点击注册广播接收器之后在点击发送会调用MyReceiver中的onReceive()方法,当用户点击注销广播接收器之后程序执行unregisterReceiver()方法
&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)Android BroadcastReceiver基础详解一 - 推酷
Android BroadcastReceiver基础详解一
-、BroadcastReceivcer概述
1、什么是广播
BroadcastReceiver是Android四大组件之一,本质是一种
全局的监听器,用于监听系统全局的广播消息。
因此它可以非常方便的实现不同组件之间的通信。 &
2、BroadcastReceiver的创建启动
BroadcastReceiver是用用于接受程序所放出的Broadcast Intent,与应用程序启动的Activity、Service相同。也只需要两步:
①、创建需要启动的Broadcast的Intent
②、创建一个类继承BroadcastReceiver,在清单文件中注册Receiver,调用content的SendBroadcast()或sendOrderedBroadcast()(发送有序广播)的方法来启动指定的BroadcastReceiver
注:当应用程序发出一个Broadcast Intent之后,所有匹配该Intent的BroadcastReceiver都会启动。
如果你不需要发送广播跨应用程序,考虑使用&这类&
LocalBroadcastManager
onCreate(Bundle savedInstanceState) {
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_startbroadcast = (Button)
.findViewById(R.id.bt_startbroadcast);
bt_startbroadcast.setOnClickListener(
View.OnClickListener() {
onClick(View v) {
Intent intent =
Intent(MainActivity.
, myBroadcast.
intent.putExtra(
&broadcast&
&hello,world! broadcast&
sendBroadcast(intent);
onReceive(Context context, Intent intent) {
Toast.makeText(context,
&receiver-----&
+ intent.getStringExtra(
&broadcast&
3、有序广播
Context.sendBroadcast
)是完全异步的。&所有的
BroadcastReceiver
是运行在一个未定义的顺序,常常在同一时间。&这时效率更高,但是意味着
BroadcastReceiver
器,不能使用结果或中止 。
Context.sendOrderedBroadcast
)交付给一个BroadcastReceiver。&因为每个BroadcastReceiver执行后返回值,它可以传播到下一个BroadcastReceiver,也可以完全通过abortBroadcast()方法终止广播,这样它就不会通过再通过后面的BroadcastReceiver两人。&在清单文件中可以控制运行&android:priority=&&&的属性匹配intent-filter;BroadcastReceiver相同的优先级将运行在一个任意的顺序。
MainActivity
Activity {
Button bt_
onCreate(Bundle savedInstanceState) {
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_start = (Button)
.findViewById(R.id.bt_start);
bt_start.setOnClickListener(
View.OnClickListener() {
onClick(View v) {
Intent intent =
intent.setAction(
intent.putExtra(
&hello,BroadcastReceivcer&
sendBroadcast(intent);
onReceive(Context arg0, Intent intent) {
String name = intent.getStringExtra(
System.out.println(
&one=======&
android:name
&com.example.broadcast_order.One&
intent-filter
android:priority
android:name
intent-filter
BroadcastReceiver {
onReceive(Context arg0, Intent intent) {
String name = intent.getStringExtra(
System.out.println(
&Two=======&
// 取消Broadcast继续传递
abortBroadcast();
android:name
&com.example.broadcast_order.Two&
intent-filter
android:priority
android:name
intent-filter
二、Receiver的生命周期
由于BroadcastReceiver本质上属于一个监听器,因此实现BroadcastReceiver的方法也十分简单,只要重写BroadcastReceiver方法中的onReceiv(Content content,Intent intent)方法即可。
每次系统Broadcast事件发生后,系统就会创建对应的BroadcastReceiver的实例,并且自动触发他的onReceive()方法,onReceive()方法执行完后,Broadcast的实例就会被销毁。也就是说Broadcast的生命周期就是onReceive()这个方法。
如果Broadcast的onReceive()方法不能在10秒内执行完成,Android会认为该程序无响应。所以不要在BroadcastReceiver的onReceive()方法中执行一些耗时操作,否则会弹出ANR的对话框。若必须要执行比较耗时的操作,则要考虑Intent的启动一个Service来完成操作。不应该考虑使用新线程去完成耗时的操作,因为BroadcastReceiver的生命周期太短了。
三、Receiver实现电池电量监控
onCreate(Bundle savedInstanceState) {
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_startbroadcast = (Button)
.findViewById(R.id.bt_startbroadcast);
bt_startbroadcast.setOnClickListener(
View.OnClickListener() {
onClick(View v) {
// 指定Broadcast能匹配Intent的方法有两种,一种是清单文件中写配置,一种是代码指定如下
IntentFilter filter =
IntentFilter(Intent.ACTION_BATTERY_CHANGED);
myBroadcast receiver =
myBroadcast();
// new出自定义BroadcastReceiver的类
registerReceiver(receiver,filter);
onReceive(Context context, Intent intent) {
(intent.getAction().equals(intent.ACTION_BATTERY_CHANGED)) {
// 获取当前电量
level = intent.getIntExtra(
// 获取总的电量
scale = intent.getIntExtra(
Toast.makeText(context,
&电池电量为:&
+ ((level *
) / scale) +
注意要在清单文件中添加获取电量权限状态的权限:&&&&&uses-permission&android:name=&android.permission.BATTERY_STATS&/&
四、开机自动运行
在自定义的BroadcastReceiver中启动activities或Service
onReceive(Context context, Intent intent) {
Intent intent1 =
Intent(context, BootActivity.
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //注册Activity时要使用setFlag()。Service不用
context.startActivity(intent1);
清单文件中注册Receiver
android:name
&com.example.BroadcastReceiverDemo.myBroadcast&
intent-filter
android:name
&android.intent.action.BOOT_COMPLETED&
android:name
&android.intent.category.HOME&
intent-filter
增加开机访问的权限:
&uses-permission&android:name=&android.permission.BATTERY_STATS&&/&
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致HDDevTeam 的BLOG
用户名:HDDevTeam
文章数:120
评论数:65
访问量:161926
注册日期:
阅读量:5863
阅读量:12276
阅读量:383104
阅读量:1074664
51CTO推荐博文
&&& 刚开始做东西得时候很不了解android中一些组件的应用方法,找了一个闹钟的小例子来更好的理解广播的接收.
&& 应用程序的界面是这样的,这个例子是用闹钟的制定与解除来穿插broadcast& Receiver 的知识,不说废话了,先看下应用程序的界面:
& 点击设定闹钟的按钮,弹出来一个设置时间的对话框,你选择时间之后会弹出一个Toast告诉你定的时间时多少,然后到时间之后弹出对话框提示时间到。先看图:
是一个小闹钟的完整过程,代码中关键的部分就是用到AlarmManager控制时间和PendingIntent 捕获要执行的广播,先贴代码再详细讲解代码。
<font color="#& &mButton1.setOnClickListener(new&View.OnClickListener()
<font color="#& &&&&{
<font color="#& &&&&&&public&void&onClick(View&v)
<font color="#& &&&&&&{&/*&取得按下按钮时的时间做为TimePickerDialog的默认值&*/
<font color="#& &&&&&&&&c.setTimeInMillis(System.currentTimeMillis());
<font color="#& &&&&&&&&startCalendar.setTimeInMillis(System.currentTimeMillis());
<font color="#& &&&&&&&&int&mHour&=&c.get(Calendar.HOUR_OF_DAY);
<font color="#& &&&&&&&&int&mMinute&=&c.get(Calendar.MINUTE);&/*&跳出TimePickerDialog来设定时间&*/
<font color="#& &&&&&&&&new&TimePickerDialog(SetAlarmActivity.this,
<font color="# &&&&&&&&&&&&new&TimePickerDialog.OnTimeSetListener()
<font color="# &&&&&&&&&&&&{
<font color="# &&&&&&&&&&&&&&public&void&onTimeSet(TimePicker&view,&int&hourOfDay,
<font color="# &&&&&&&&&&&&&&&&&&int&minute)
<font color="# &&&&&&&&&&&&&&{&/*&取得设定后的时间,秒跟毫秒设为0&*/
<font color="# &&&&&&&&&&&&&&&&c.setTimeInMillis(System.currentTimeMillis());
<font color="# &&&&&&&&&&&&&&&&c.set(Calendar.HOUR_OF_DAY,&hourOfDay);
<font color="# &&&&&&&&&&&&&&&&c.set(Calendar.MINUTE,&minute);
<font color="# &&&&&&&&&&&&&&&&c.set(Calendar.SECOND,&<span style="color: #);
<font color="# &&&&&&&&&&&&&&&&c.set(Calendar.MILLISECOND,&<span style="color: #);
<font color="# &&&&&&&&&&&&&&&&Intent&intent&=&new&Intent(SetAlarmActivity.this,
<font color="# &&&&&&&&&&&&&&&&&&&&CallAlarm.class);
<font color="# &&&&&&&&&&&&&&&&PendingIntent&sender&=&PendingIntent.getBroadcast(
<font color="# &&&&&&&&&&&&&&&&&&&&SetAlarmActivity.this,&<span style="color: #,&intent,&<span style="color: #);
<font color="# &&&&&&&&&&&&&&&&AlarmManager&
<font color="# &&&&&&&&&&&&&&&&am&=&(AlarmManager)&getSystemService(ALARM_SERVICE);
<font color="# &&&&&&&&&&&&&&&&am.set(AlarmManager.RTC_WAKEUP,&c.getTimeInMillis(),
<font color="# &&&&&&&&&&&&&&&&&&&&sender);&/*&更新显示的设定闹钟时间&*/
<font color="# &&&&&&&&&&&&&&&&String&tmpS&=&format(hourOfDay)&+&&:&&+&format(minute);
<font color="# &&&&&&&&&&&&&&&&setTime1.setText(tmpS);&/*&以Toast提示设定已完成&*/
<font color="# &&&&&&&&&&&&&&&&Toast.makeText(SetAlarmActivity.this,&&设定闹钟时间为&&+&tmpS,
<font color="# &&&&&&&&&&&&&&&&&&&&Toast.LENGTH_SHORT).show();
<font color="#
<font color="# &&&&&&&&&&&&&&}
<font color="# &&&&&&&&&&&&},&mHour,&mMinute,&true).show();
<font color="# &&&&&&}
<font color="# &&&&});
代码中显示了当点击按钮之后所进行的代码处理,我们获得了设置的时间,然后用了一个Intent,用了一个pendIntent,这个PendIntent在捕获广播的时候才会启动,然后我们将这个PendIntent绑定到AlarmManager上面,有三个参数,一个是时间格式,一个是响铃的时间,一个是闹铃到期时启动sender这个PendIntent对象,然后启动绑定在PendIntent里面的intent,从而启动广播CallAlarm。
在广播接收器CallAlar中,并不是直接执行代码,广播接收器只有一个onReceive方法,在这个方法里面又启动了另外一个Activity。需要说明的是广播接收器是没有界面的.
<font color="#& package&com.&/*&import相关class&*/
<font color="#&
<font color="#& import&android.content.C
<font color="#& import&android.content.I
<font color="#& import&android.content.BroadcastR
<font color="#& import&android.os.B&/*&调用闹钟Alert的Receiver&*/
<font color="#&
<font color="#& public&class&CallAlarm&extends&BroadcastReceiver
<font color="#& {
<font color="# &&@Override
<font color="# &&public&void&onReceive(Context&context,&Intent&intent)
<font color="# &&{&/*&create&Intent,调用AlarmAlert.class&*/
<font color="# &&&&Intent&i&=&new&Intent(context,&AlarmAlert.class);
<font color="# &&&&Bundle&bundleRet&=&new&Bundle();
<font color="# &&&&bundleRet.putString(&STR_CALLER&,&&&);
<font color="# &&&&i.putExtras(bundleRet);
<font color="# &&&&i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
<font color="# &&&&context.startActivity(i);
<font color="# &&}
<font color="# }
启动另外一个Activity,是弹出闹铃时间到时的提示对话框,看下代码:
<font color="#& package&com.&/*&import相关class&*/
<font color="#&
<font color="#& import&android.app.A
<font color="#& import&android.app.AlertD
<font color="#& import&android.content.DialogI
<font color="#& import&android.os.B
<font color="#&
<font color="#& public&class&AlarmAlert&extends&Activity
<font color="#& {
<font color="# &&@Override
<font color="# &&protected&void&onCreate(Bundle&savedInstanceState)
<font color="# &&{
<font color="# &&&&super.onCreate(savedInstanceState);&/*&跳出的闹铃警示&*/
<font color="# &&&&new&AlertDialog.Builder(AlarmAlert.this).setIcon(R.drawable.clock)
<font color="# &&&&&&&&.setTitle(&闹钟响了!!&).setMessage(&赶快起床吧!!!&).setPositiveButton(
<font color="# &&&&&&&&&&&&&关掉他&,&new&DialogInterface.OnClickListener()
<font color="# &&&&&&&&&&&&{
<font color="# &&&&&&&&&&&&&&public&void&onClick(DialogInterface&dialog,
<font color="# &&&&&&&&&&&&&&&&&&int&whichButton)
<font color="# &&&&&&&&&&&&&&{&/*&关闭Activity&*/
<font color="# &&&&&&&&&&&&&&&&AlarmAlert.this.finish();
<font color="# &&&&&&&&&&&&&&}
<font color="# &&&&&&&&&&&&}).show();
<font color="# &&}
<font color="# }
如果条件允许,还可以在响铃时加上震动,铃声等选项..
附上源码.本文出自 “” 博客,请务必保留此出处
了这篇文章
附件下载:  
类别:┆阅读(0)┆评论(0)
请输入验证码:

我要回帖

更多关于 安卓启动receiver 的文章

 

随机推荐