9

我试图弄清楚如何使用服务唤醒和解锁手机。我一直在参考这篇文章,但我不知道为什么它不起作用。这是我到目前为止的代码:

public class WakephoneActivity extends Activity {

    BroadcastReceiver mReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // Log.v(TAG, "Screen OFF onReceive()");
                screenOFFHandler.sendEmptyMessageDelayed(0, 2000);
            }
        };
    }

    private Handler screenOFFHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // do something
            // wake up phone
            // Log.i(TAG, "ake up the phone and disable keyguard");
            PowerManager powerManager = (PowerManager) WakephoneActivity.this
                    .getSystemService(Context.POWER_SERVICE);
            long l = SystemClock.uptimeMillis();
            powerManager.userActivity(l, false);// false will bring the screen
            // back as bright as it was, true - will dim it
        }
    };

    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);
        // Log.i(TAG, "broadcast receiver registered!");
    }
}

我也在清单中添加了代码。有任何想法吗?

4

3 回答 3

15

在您的服务中使用以下代码。

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
        mWakeLock.acquire();
        [...]
        mWakeLock.release();

如果您还想解锁屏幕,请在您的服务中注册一个接收器,以监控屏幕是否打开/关闭,如果屏幕已关闭并且您想解锁手机,请使用以下代码启动活动onCreate

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
this.finish();
return;

我知道,这是一个相当肮脏的,但据我所知,没有其他解锁锁屏的方法(这只有在没有设置密码等情况下才有效,所以它必须是正常的“滑动解锁”屏幕)。

并且不要忘记添加android.permission.WAKE_LOCK;-)

/edit:我刚刚看到你已经在使用一个活动。如果您有一个并且根本不需要该服务,只需将此代码放入活动中。

于 2011-11-21T17:15:05.547 回答
1

要使服务始终处于活动状态,您需要对清单具有此权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

您需要做的另一件事是购买 WakeLock。没有它,服务将结束一段时间。你可以这样做:

getApplicationContext();
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();

您可能需要将 PowerManager.PARTIAL_WAKE_LOCK 更改为您需要的那个。您可以在此处查看相关信息。

于 2011-11-10T00:35:25.307 回答
1

WakefulBroadcastReceiver哪个可以为您做到这一点。示例使用:

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }
}

在服务中完成动作后,调用 SimpleWakefulReceiver.completeWakefulIntent(intent)释放唤醒锁。

(因为@Force 已经为您提供了有关唤醒锁的详细信息,因此无需在此重复;-)

请注意,该类已从 api 级别 26.1.0 弃用,请参考此处

于 2016-01-27T12:45:46.783 回答