我为adnroid设计了一个锁屏.当用户按下电源按钮解锁时,我正在尝试使用广播接收器来启动锁屏活动.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
}
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>
Run Code Online (Sandbox Code Playgroud)
` …