报警管理器不会触发(Alarm Manager wont trigger)

编程入门 行业动态 更新时间:2024-10-27 06:33:14
报警管理器不会触发(Alarm Manager wont trigger) java

昨天我问了一个类似于这个的问题,但是我的代码已经改变了一些,现在有了不同的问题。

我有一个切换按钮,设置一个具有待定意图的警报管理器,该意图应在5秒后触发。 我有一次性设置,所以我希望消息出现一次(后面我将实现这个日期值)。

我没有得到这个代码的错误,但我似乎无法触发我的意图,然后显示我的吐司消息。

以下是我在xml中定义'DateAlarm'类的活动的方法:

<activity android:name=".DateAlarm" android:label="@string/app_name" > <intent-filter> <action android:name="com.example.flybase2.DateAlarm" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

继承我的切换按钮onClick方法,我的切换按钮与警报管理器:

case (R.id.toggleButton1): Integer dobMonth = setDate.getMonth(); Integer dobYear = setDate.getYear(); Integer dobDate = setDate.getDayOfMonth(); Date set; set = new Date(dobYear - 1900, dobMonth, dobDate); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, DateAlarm.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (5 * 1000), pendingIntent); break;

我最后的'DateAlarm'类包含了intent的动作。

package com.example.flybase2; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.Toast; public class DateAlarm extends Activity { Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(context, "Appointment is today", Toast.LENGTH_LONG).show(); }

}

I asked a question similar to this yesterday but have changed my code a fair bit and have a different issue now.

I have a toggle button that sets an alarm manager with a pending intent that should trigger after 5 seconds. I have it on a one shot setting, so i want the message to appear once (as later I will be implementing this for a date value).

I get no errors with this code, but I cant seem to trigger my intent that then shows my toast message.

Here's how I have defined the activity of the 'DateAlarm' class in the xml:

<activity android:name=".DateAlarm" android:label="@string/app_name" > <intent-filter> <action android:name="com.example.flybase2.DateAlarm" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

Heres my my toggle button onClick method for my toggle button with the alarm manager:

case (R.id.toggleButton1): Integer dobMonth = setDate.getMonth(); Integer dobYear = setDate.getYear(); Integer dobDate = setDate.getDayOfMonth(); Date set; set = new Date(dobYear - 1900, dobMonth, dobDate); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, DateAlarm.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (5 * 1000), pendingIntent); break;

And my final 'DateAlarm' class that holds the action of the intent.

package com.example.flybase2; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.Toast; public class DateAlarm extends Activity { Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(context, "Appointment is today", Toast.LENGTH_LONG).show(); }

}

最满意答案

你有两个问题:

您正在使用PendingIntent.getService()而不是PendingIntent.getActivity()因为DateAlarm扩展了Activity ,所以您需要PendingIntent.getActivity() 。

在DateAlarm您的上下文变量为null。 在onCreate()中,您应该初始化它。 另请注意,由于Activity扩展了Context ,因此不需要此变量。 但是,如果你想使用该变量,请执行以下操作:

super.onCreate(savedInstanceState); context = this;

要么

Context context = this; //right from the global declaration

You have two problems:

You are using PendingIntent.getService() instead of PendingIntent.getActivity() Since DateAlarm extends Activity, you want PendingIntent.getActivity().

In DateAlarm your context variable is null. In onCreate() you should initialize it. Also note that since Activity extends Context, this variable isn't needed. However if you do want to use that variable do:

super.onCreate(savedInstanceState); context = this;

or

Context context = this; //right from the global declaration

更多推荐

本文发布于:2023-08-07 19:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   Alarm   Manager   wont   trigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!