我的应用程序是某种闹钟。我希望它能够从锁定屏幕唤醒到警报活动中。 活动:
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class AlarmReceiverActivity extends Activity {
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock wl;
String titl;
private static boolean active=false;
public static boolean isActive() {
return active;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
active=true;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Log.d("alarm", "got here");
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.alarm);
TextView text = (TextView)findViewById(R.id.tvt);
Intent inte1 = getIntent();
text.setText(inte1.getExtras().getCharSequence("titl"));
Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
mMediaPlayer.stop();
Intent ss = new Intent(AlarmReceiverActivity.this,BackgroundLocationService.class);
stopService(ss);
Intent i =new Intent(AlarmReceiverActivity.this,MnaActivity.class);
startActivity(i);
return false;
}
});
playSound(this, getAlarmUri());
}
@Override
protected void onStop() {
super.onStop();
active=false;
wl.release();
}
private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
System.out.println("OOPS");
}
}
@Override
public void onBackPressed()
{
mMediaPlayer.stop();
Intent ss = new Intent(AlarmReceiverActivity.this,BackgroundLocationService.class);
stopService(ss);
Intent i =new Intent(AlarmReceiverActivity.this,MnaActivity.class);
startActivity(i);
}
//Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
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;
}
}
当我从锁定屏幕测试它时,它工作正常,直到我点击stopAlarm按钮然后它崩溃。但是,当这个活动没有从锁定屏幕打开时,它就像它应该的那样工作。我在logcat中得到了这个错误:
06-03 10:14:09.921: E/AndroidRuntime(2411): FATAL EXCEPTION: main
06-03 10:14:09.921: E/AndroidRuntime(2411): Process: com.example.fgps, PID: 2411
06-03 10:14:09.921: E/AndroidRuntime(2411): java.lang.RuntimeException: Unable to stop activity {com.example.fgps/com.example.fgps.AlarmReceiverActivity}: java.lang.RuntimeException: WakeLock under-locked My Tag
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3188)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread.access$1100(ActivityThread.java:135)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.os.Looper.loop(Looper.java:136)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 10:14:09.921: E/AndroidRuntime(2411): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 10:14:09.921: E/AndroidRuntime(2411): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 10:14:09.921: E/AndroidRuntime(2411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 10:14:09.921: E/AndroidRuntime(2411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 10:14:09.921: E/AndroidRuntime(2411): at dalvik.system.NativeStart.main(Native Method)
06-03 10:14:09.921: E/AndroidRuntime(2411): Caused by: java.lang.RuntimeException: WakeLock under-locked My Tag
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.os.PowerManager$WakeLock.release(PowerManager.java:764)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.os.PowerManager$WakeLock.release(PowerManager.java:735)
06-03 10:14:09.921: E/AndroidRuntime(2411): at com.example.fgps.AlarmReceiverActivity.onStop(AlarmReceiverActivity.java:74)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.Activity.performStop(Activity.java:5376)
06-03 10:14:09.921: E/AndroidRuntime(2411): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3185)
06-03 10:14:09.921: E/AndroidRuntime(2411): ... 11 more
我该如何解决?