如何在解锁屏幕时启动服务?也许就像AlarmAanger!
context.startService(new Intent(context, Widget.class));
Run Code Online (Sandbox Code Playgroud)
对于检测屏幕开启和屏幕关闭注册,广播收发器如:
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>
Run Code Online (Sandbox Code Playgroud)
在活动或服务中:
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) {
}
Run Code Online (Sandbox Code Playgroud)
接收器代码,系统通知您是否发生屏幕开/关:
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)){
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
129 次 |
最近记录: |