关闭手机或重新启动以及关闭所有应用时,我的服务不会运行

时间:2017-07-29 05:23:11

标签: android service background-service

我每秒使用一项服务来检查我的数据库,如果时间现在等于数据库显示通知的时间,但是当重启或关机或重新启动我的手机服务时杀死而不显示通知这是我的代码:

AndroidManifest.xml文件的内容:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <receiver
                android:name=".service.BootCompleteReceiver"
                android:enabled="true">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </receiver>

<service
            android:name=".service.MyService"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED"/>

BootComplate Receiver类的内容:

Intent service = new Intent(context, MyService.class);
    context.startService(service);

并在MyService类中编写此代码:

public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

但这不起作用。请帮助解决这个问题。

0 个答案:

没有答案