具有待处理意图的警报管理器

2024-06-03

下面的代码片段......

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 之后运行该程序时,我收到一条错误“应用程序意外停止”。为什么我会收到此错误?

我的目标是每 9sn 显示一次文本。正确的用法是什么AlarmManager在主要活动中
或者我应该在BroadcastReceiver? 哪一个确实有意义:am.setRepeating or am.set就我的目标而言?

**编辑:如何更改警报代码以在广播接收器中运行? **


//尝试这个

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有待处理意图的警报管理器 的相关文章

随机推荐