解锁屏幕时如何启动服务?也许像 AlarmAanger !
context.startService(new Intent(context, Widget.class));
要检测屏幕开启和屏幕关闭,请注册广播接收器,例如:
AndroidManifest.xml:
<receiver android:name="receiverScreen">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
在活动或服务中:
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new receiverScreen();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
如果屏幕开/关发生,系统会通知您的接收器代码:
public class receiverScreen extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
}
}