我想实现一个处理屏幕开/关的服务。为此,我创建BootReceiver
了在启动完成时调用并在我启动服务的过程中。
但是,OnCreate
从来没有调用过,为什么?
当我打印程序的日志时,onCreate
即使我看到了onStartCommand
' 的日志,我也永远不会看到 ' 的日志。这怎么可能?请帮助我理解这一点。
这是BootReceiver
一开始就调用的:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "BootReceiver");
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
}
}
这是服务:
public class ScreenListenerService extends Service {
public void OnCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("TAG", "ScreenListenerService---onStart ");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
和清单:
<service android:name=".ScreenListenerService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>