1

我想做一个BroadcastReceiver,以便在屏幕关闭时收到通知,然后我再次打开它,但它什么也没做,有什么问题?

public class SplashScreen extends SherlockActivity {
PowerManager.WakeLock mWakeLock;

BroadcastReceiver rec = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            WakeLock screenLock = ((PowerManager) getSystemService(POWER_SERVICE))
                    .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
            screenLock.acquire();

        }

    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.sensounlock.R.layout.rotation);

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    registerReceiver(rec, filter);
}
4

1 回答 1

0

移动你的

BroadcastReceiver rec = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            WakeLock screenLock = ((PowerManager) getSystemService(POWER_SERVICE))
                    .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
            screenLock.acquire();

        }

    }
};

在你的 oncreate 下

public class SplashScreen extends SherlockActivity {
PowerManager.WakeLock mWakeLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.sensounlock.R.layout.rotation);

BroadcastReceiver rec = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            WakeLock screenLock = ((PowerManager) getSystemService(POWER_SERVICE))
                    .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
            screenLock.acquire();

        }

    }
};
        registerReceiver(rec, new IntentFilter("package.name.xx"));
        pi = PendingIntent.getBroadcast(this, 0, new Intent(
                "package.name.xx"), 0);
}
于 2013-09-01T17:50:46.077 回答