0

如果屏幕关闭,我编写了这段代码来唤醒我的活动。

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());}

问题是当屏幕关闭时,应用程序被暂停。

4

3 回答 3

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"
于 2013-06-21T11:34:01.247 回答
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();
            }
        }
    });

只记得在不使用时释放它

于 2014-02-13T18:02:49.643 回答
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();
    }
}

如果您还有其他疑问,请告诉我。准备提供帮助!

于 2014-06-26T11:52:23.853 回答