AlarmMananger 似乎没有执行 - 为什么?
我有以下代码来触发警报,当警报触发时,意味着启动另一个基本上是午睡警报的活动。经过谷歌和SO的大量搜索后,我不知所措。我相信我对 AlarmManager
有正确的设置,但它应该启动的 Activity
从未启动。
顺便说一句,最终我想使用 napTime
变量来设置 AlarmManager
触发的时间,但出于测试目的,我只是试图让它触发正道。
这是设置小睡的代码。 Log.e("Nap time", "" + napTime);
行始终显示为napTime 选择的正确数字,因此我知道该方法正在执行。
private void setNap(int napTime) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), NapAlarm.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
Log.e("Nap time", "" + napTime);
TextView nap = (TextView) findViewById(R.id.nap);
nap.setText("nap set");
}
以及启动的活动的代码:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NapAlarm extends Activity implements OnClickListener {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Button dButton = (Button) findViewById(R.id.dismissButton);
dButton.setOnClickListener(this);
Button sButton = (Button) findViewById(R.id.snoozeButton);
sButton.setOnClickListener(this);
new Thread(new Runnable() {
public void run() {
try {
Log.e("Got this far", "In the alarm");
mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
mp.setLooping(true);
} catch (Exception e) {
}
}
}).start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dismissButton:
mp.stop();
this.finish();
break;
case R.id.snoozeButton:
Button sButton = (Button) findViewById(R.id.snoozeButton);
sButton.setText("I'm sorry, this app currently does not support snoozing, you must wake up.");
break;
}
}
}
如果我单独运行第二段代码,它会工作并且会播放声音,等等。但是如果我从 AlarmMananger
的较大项目中运行它> 应该创建一个警报来触发它,但它从未发生。
有什么我忽略的事情吗?
I have the following code to trigger an alarm, which when it fires is meant to launch another activity that is basically a nap alarm. After much searching through Google and SO, I am at a loss. I believe I have the correct setup for the AlarmManager
, but the Activity
that it is supposed to launch never gets launched.
By the way, eventually I want to use the napTime
variable to set the time at which the AlarmManager
will fire, but for testing purposes I am just trying to get it to fire right way.
Here is the code to set the nap. The Log.e("Nap time", "" + napTime);
line always shows the correct number that was selected for the napTime, so I know that the method is executing.
private void setNap(int napTime) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), NapAlarm.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
Log.e("Nap time", "" + napTime);
TextView nap = (TextView) findViewById(R.id.nap);
nap.setText("nap set");
}
And the code for the activity that is launched:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NapAlarm extends Activity implements OnClickListener {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Button dButton = (Button) findViewById(R.id.dismissButton);
dButton.setOnClickListener(this);
Button sButton = (Button) findViewById(R.id.snoozeButton);
sButton.setOnClickListener(this);
new Thread(new Runnable() {
public void run() {
try {
Log.e("Got this far", "In the alarm");
mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
mp.setLooping(true);
} catch (Exception e) {
}
}
}).start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dismissButton:
mp.stop();
this.finish();
break;
case R.id.snoozeButton:
Button sButton = (Button) findViewById(R.id.snoozeButton);
sButton.setText("I'm sorry, this app currently does not support snoozing, you must wake up.");
break;
}
}
}
If I run the second piece of code by itself, it works and the sound plays, etc. But if I run it from within the larger project from which the AlarmMananger
is supposed to create an alarm to trigger it, it never happens.
Is there something that I am overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 NapTime 活动应该像这样扩展广播接收器。
确保您像这样注册接收器。
请参阅此处了解更多信息
报警管理器教程
Your NapTime activity should be extending a BroadCast Receiver like this..
Make sure you register your reciever like this..
Refer here for more info
Alarm Manager tutorial