Android中BroadcastReceiver如何使用Intent
来源:互联网 发布:网络运营商选择 编辑:程序博客网 时间:2024/06/05 01:12
这一天要做一个定时打电话功能的app
前面的主Activity中,从TimePicker中获取设定的时间,从EditText中获取电话号码
使用Calendar类用获取得到的时间初始化一个calendar对象
将其转换成毫秒数(因为AlarmManager设定时间要求是毫秒数)
在Intent中设定转移的class,加上时间数据
String number = edtCallNumber.getText().toString(); Intent intent = new Intent(MainActivity.this, timeReceiver.class); intent.putExtra("CallNumber", number); //设置Action intent.setAction("com.fish.TIMINGCALL"); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0, intent, 0); //设置一次提醒闹钟 alarmManager.set(AlarmManager.RTC_WAKEUP, setTime, pendingIntent);</span>
alarmManager到达setTime时间之后就会发送一条“com.fish.TIMINGCALL“的广播
而到BroadcastReceiver中,重写的onReceiver函数获取要拨打的电话号码
String callNumber = intent.getStringExtra("CallNumber");</span>
我们知道,拨打电话功能一般都是使用Intent来执行,然而开启Intent的函数startActivity却报错
仔细看看发现,这个函数是需要Context也即是上下文,而这个函数本身就不是一个Acticity,所以会报错
那怎么办呢
仔细观察onReceiver函数我们能发现,这个函数除了传进来一个Intent对象,还有一个Context对象,而Activity正是Context的子类
所以,我猜测应该可以用Context启动Intent
context.startActivity(callIntent);</span>
好的没有报错,运行,发现停止工作,最后查询资料才发现,应该先要往要启动的Intent添加一个Flags
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</span>
再次运行,程序运行正常
0 0
- Android中BroadcastReceiver如何使用Intent
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android之Intent&BroadcastReceiver
- Android中广播BroadCastReceiver使用
- Android中BroadcastReceiver的使用
- Android中BroadcastReceiver的使用
- Android中BroadCastReceiver的使用
- android 中Intent 使用
- Android - Intent、PendingIntent、IntentFilter、BroadcastReceiver
- 转载:Android中BroadCastReceiver使用(整理)
- Android中BroadCastReceiver使用(整理)
- 举例说明android中BroadcastReceiver的使用
- 大数据处理之道 (MATLAB 篇)
- 数据结构--线性表--双向链表
- vim删除文本的命令
- 野生码农狂奔中的2014年度年终总结
- 最全的Eclipse快捷键
- Android中BroadcastReceiver如何使用Intent
- sphinx php api使用讲解
- 彩票审计风暴起 万亿巨资去那里?
- 安慰奶牛
- linux find command
- 2.ios拖控件
- 我的c语言笔记---字符串
- 设计模式 观察者模式
- 代码高亮之prettify