我正在开发一个可以在屏幕上创建闹钟的应用程序。我按下按钮的每一个小时。我有一个类,我定义了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);
}
答案 0 :(得分:1)
您是否可以尝试将PowerManager代码更改为此类代码,以确定其是否有效:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "");
mWakeLock.acquire();