我编写了这段代码,以便在屏幕关闭时唤醒我的活动。
private PowerManager mPM;
private PowerManager.WakeLock mPartialWakeLock;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//some code
mPM = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
if (mPM == null) {
Log.e(TAG, "PowerManager is null");
}
try {
mPartialWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , "LOG");
mPartialWakeLock.aquire();
}
catch (Exception e) {Log.i(TAG, "mPM.newWakeLock() EXCEPTION="+e.toString());}
问题是当屏幕关闭时,应用程序暂停。
答案 0 :(得分:0)
如果要在活动名称
时解锁屏幕,请尝试使用此选项Window wind;
wind = this.getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
如果你阻止屏幕锁定,那么只需将以下代码写入xml文件就可以防止屏幕锁定
android:keepScreenOn="true"
答案 1 :(得分:0)
我目前正在应用程序中使用以下部分唤醒锁定:
public class my_frag extends Fragment {
WakeLock wl;
//on create, config changed, etc
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
PowerManager pm = (PowerManager) this.getActivity().getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
//I happen to have it in a button click event based on an async task
//(Side note: I should probably be using a Loader for my Async task but this works too)
//Just move it outside the button click if you don`t need it there
connectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (metrics_task != null)
{
Status s = metrics_task.getStatus();
if (s.name().equals("RUNNING")){
if (metrics_task.running){
metrics_task.cancel(true);
connectButton.setText("Start");
metrics_task.running = false;
wl.release(); <--releases it on async stop
}
else{
metrics_task = new start_metrics(ae);
metrics_task.execute();
wl.acquire(); <--starts it on async start
}
}
else{
metrics_task = new start_metrics(ae);
metrics_task.execute();
}
}
else{
metrics_task = new start_metrics(ae);
metrics_task.execute();
}
}
});
请记住在不使用时将其发布
答案 2 :(得分:0)
尝试通过扩展Application类来获取唤醒锁,这意味着为整个应用程序获取了锁: 代码:
package com.ballytech.RemoteGamingClient.UserView;
import android.app.Application;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
/**
* @author SDurai
*
*/
public class RCGApplication extends Application
{
private static final String TAG = RCGApplication.class.getSimpleName();
private PowerManager.WakeLock mWakeLock = null;
@Override
public void onCreate() {
super.onCreate();
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire();
}
@Override
public void onTerminate() {
if (mWakeLock.isHeld())
mWakeLock.release();
super.onTerminate();
}
}
如果您有任何其他疑问,请告诉我。随时准备帮助!