屏幕打开时开始 activity

Start an activity when screen on

我为adnroid设计了一个锁屏。当用户按下电源按钮解锁时,我正在尝试使用广播接收器启动锁屏 activity。

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
        Log.w("BAM", "Screen went on");
    }

    else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
        Log.w("BAM","Screen went off");
    }
}

}

android 清单:`

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyLockScreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name="MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/admin"/>

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".Receiver">
      <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.intent.action.ACTION_SHUTDOWN" />
     </intent-filter>
    </receiver>
    <activity android:name=".LockScreen"></activity>
</application>

`

private ActionBar actionBar;
private ViewPager viewer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.lockscreen);
    actionBar = getSupportActionBar();
    actionBar.hide();

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);

    viewer = (ViewPager) findViewById(R.id.viewr);
    viewer.setAdapter(new MyAdapter(getSupportFragmentManager()));
}

但是当用户按下电源按钮时,锁屏 activity 会在 1-3 秒后启动。 当用户先按下电源按钮关闭屏幕时启动 activity 是否合适?我该怎么做?

感谢您的建议。 (抱歉我的英语不好!)

First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! I’m not sure exactly why, but they must be registered in an IntentFilter in your JAVA code

使用:

再次编辑

public class YourActivity extends Activity {

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

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    public class ScreenReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                Log.w("BAM", "Screen went on");
            }

            else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                Log.w("BAM","Screen went off");
            }
        }
    }
} 

已测试,成功!