使用Broadcastreceiver重新启动后重新启动服务不起作用

时间:2018-07-14 09:22:40

标签: android service broadcastreceiver reboot

我要按照此网站Example的说明,在设备重启后自动启动服务。 创建了BroadCastReceiverBootUpComplete类,以在重新启动后重新启动服务(MyService.java),但它根本没有启动。 我希望MyService.java在设备重新启动后立即运行。 MainActivity.java

package com.example.shubham.servicedemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this,MyService.class));
    }
}  

BroadCastReceiverOnBootComplete.java

package com.example.shubham.servicedemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BroadCastReceiverOnBootComplete extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){
            Intent serviceIntent  = new Intent(context,MyService.class);
            context.startService(serviceIntent);
        }
    }
}  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shubham.servicedemo">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.example.shubham.servicedemo.BroadCastReceiverOnBootComplete"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>

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

</manifest>  

1 个答案:

答案 0 :(得分:0)

如果您的目标是上面的android版本26,则必须使用startforeground服务

 Intent mIntentForService = new Intent(context, MyService.class);
    mIntentForService.setAction(intent.getAction());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        context.startForegroundService(mIntentForService);
    else
        context.startService(mIntentForService);

检查下面的链接以获取更多详细信息

link

请注意,即使您的服务实际上并未 曾经致电startForeground()。您有时间去逛逛 调用startForeground(),“相当于ANR间隔”。 如果您的工作时间超过一毫秒,但少于几秒钟, 您可以跳过NotificationstartForeground()通话。