我正在开发一个可以在屏幕上创建警报的应用程序。每隔一小时,当我按下一个按钮。我有一个类,我在其中定义了 AlarmManager、WakeLock 和一个在触发警报时将全屏打开的窗口,并且我从 MainActivity.class 启动它。但我没有错误,警报根本不会运行。我是新手,所以请指出正确的方向。我已经完成了研究,但我根本找不到我的代码有什么问题。
MyAlarmReceiverActivity 代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.alarm);
Button stopAlarm = (Button) findViewById(R.id.btnStopAlarm);
stopAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
mMediaPlayer.stop();
finish();
}
});
playSound(this, getAlarmUri());
}
private void playSound(Context context, Uri alert)
{
mMediaPlayer = new MediaPlayer();
try
{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!=0)
{
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}catch (IOException e)
{
Log.i("AlarmReceiver","No audio files are found!");
}
}
private Uri getAlarmUri(){
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
protected void onStop()
{
super.onStop();
mWakeLock.release();
}
在我的主要活动中,我正在使用此代码运行它
public void spusteniePripomienok (){
Toast.makeText(this, "Spustil si pripomienky každých "+ interval, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, AlarmRecieverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT );
AlarmManager am = (AlarmManager) getSystemService(MainActivity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
}