解锁屏幕时如何启动服务?也许就像AlarmAanger!
context.startService(new Intent(context, Widget.class));
答案 0 :(得分:6)
用于检测屏幕开启和屏幕关闭注册广播接收器,如:
的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)){
}
}
}