Alarmmanager与未决的意图 [英] Alarmmanager with pending Intent
本文介绍了Alarmmanager与未决的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下code片段......
The code snippet below....
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
///////////Do something////////////////////////
showtext.startScan();
//SEt Alarm
Intent intent = new Intent(this, TextReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager am=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+9000, pi);}
和我的接收机
TextReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
///Show text/////
}
}
的事情是,当我9sn后运行程序,我该得到一个错误该应用程序停止unexpextdly。 。我的目标是显示文本每9sn
为什么我得到这个错误?
什么是alarmmanager在主要活动的正确用法?
或者我应该设置在广播接收器报警?
哪一个不使我的目标而言意义am.setRepeating或am.set?
The thing is that when I run the program after 9sn, I am getting an error that "The app stopped unexpextdly." . My goal is to show the text every 9sn Why I get this error ? What is the correct usage of alarmmanager in the main activity ? OR Should I set alarm in the BroadcastReceiver ? Which one does make sense am.setRepeating or am.set in terms of my goal ?
*的编辑:我怎样才能改变我的报警code在广播接收器上运行? * 的
推荐答案
//试试这个
AlarmManager am=(AlarmManager)getApplicationContext getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(9 * 1000), pendingIntent);
这篇关于Alarmmanager与未决的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文