昨天我问了一个类似于这个的问题,但是我的代码已经改变了一些,现在有了不同的问题。
我有一个切换按钮,设置一个具有待定意图的警报管理器,该意图应在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 declarationYou 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更多推荐
发布评论