即使手机处于锁定模式,我如何开始活动,这样我才能访问我的应用程序?
我想首先使用ringAlarm()
响铃,然后转到另一个显示alertdialog
以停止闹钟的活动。
如果手机处于活动状态,它可以正常工作,但是当手机被锁定时,它会持续报警,当我解锁手机时,停止闹铃按钮不会启动。请帮我。提前感谢您的帮助。
这是我的代码:
public class EAlarmReceiver extends BroadcastReceiver {
public static String sender;
public static String sms = "";
public static Ringtone r;
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++)
{
messages[i] = SmsMessage.createFromPdu ((byte[])
pdusObj[i]);
sender = messages[i].getOriginatingAddress();
sms = messages[i].getMessageBody();//save sms to string
}
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("alert")) {
Intent openStopAlarm = new Intent("proj.receiver.STOPALARM");
openStopAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
ringAlarm(context);
context.startActivity(openStopAlarm);
abortBroadcast();
}//end if
}//end for
}// end onreceive
//ring the alarm
public void ringAlarm(Context context)
{
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){ //
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
}
//end ringAlarm()
}
我见过如何做的代码,但我不知道如何使用它。所以我仍然遇到同样的问题。这是代码。
public class DismissLock extends Activity {
PowerManager pm;
WakeLock wl;
KeyguardManager km;
KeyguardLock kl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("INFO", "onCreate() in DismissLock");
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
kl= km.newKeyguardLock("INFO");
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire(); //wake up the screen
kl.disableKeyguard();// dismiss the keyguard
setContentView(R.layout.main);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
wl.acquire();//must call this!
}
}
答案 0 :(得分:7)
您只需要设置一些额外的窗口标志:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
答案 1 :(得分:2)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
粘贴您要在锁定屏幕上执行的活动中的代码