制作一个报警程序 - 不工作 [英] Making an alarm program - not working
问题描述
我试图让报警程序。到目前为止,我已经写在其中,用户可以选择他希望闹钟启动时的活动。这是工作的罚款。现在我需要使用报警经理告诉OS打电话给我的一些code在某一点在未来。只是在我补充说,被执行时,我美元的我的主要活动p $ PSS测试按钮下面的code原始方法测试:
I am attempting to make alarm program. So far I have written an activity in which the user can select the time he wishes the alarm to go off. This is working fine. Now I need to use the alarm manger to tell the OS to call some of my code at a certain point in the future. Just to test this in a crude way I added the following code that gets executed when I press a test button in my main activity:
Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.cancel(pIntent);
alarms.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+1000,
AlarmManager.INTERVAL_DAY,
pIntent);
这应该意味着一些code称为to_call_when_alarm_goes_off会得到执行后一秒我preSS按钮....现在,这是我有点困惑。我不知道怎么挺/在哪里设立to_call_when_alarm_goes_off。我所做的只不过是添加一个新类到我的项目如下:
This should mean that some code called to_call_when_alarm_goes_off will get executed one second after I press the button.... Now this is where I'm a little confused. I'm not sure quite how/where to set up "to_call_when_alarm_goes_off". What I did was simply add a new class to my project as follows:
package com.mycompany.alarmprogram;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class to_call_when_alarm_goes_off extends BroadcastReceiver
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
// TODO Auto-generated method stub
Log.i("ALARM","TIME TO WAKE UP!!!");
}
}
所有code编译,当我preSS按钮即code在第一code段没有崩溃被执行 - 但一秒钟后,广播接收器code不执行。显然,我误解的东西。
All the code compiles, and when I press the button all the code in the first code snippet gets executed without crashing - but one second later the broadcast receiver code is not executed. Clearly I am misunderstanding something.
推荐答案
我认为你缺少注册您的接收器清单文件,通过适当的操作字符串。如下面给出的。
I assume you are missing registering your receiver in Manifest file, With appropriate action string. as given below.
<receiver android:name=".to_call_when_alarm_goes_off" >
<intent-filter>
<action android:name="com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE" />
</intent-filter>// can change name/action string as par ur requirement.
</receiver>
您需要将相同的动作字符串中你的意图,记住动作字符串必须清单这里 intent.setAction(com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE)相同;
在java中也。那么只有将tringger接收器。
you need to set same action string in your intent, Remember Action string must be same in Manifest and here intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE");
in java also. then only it will tringger receiver.
您code能像如下改变。
Your code can be changed like given below.
Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE");// added line
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.cancel(pIntent);
alarms.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+1000,
AlarmManager.INTERVAL_DAY,
pIntent);
这篇关于制作一个报警程序 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!