【问题标题】:Restarting service after reboot by using Broadcastreceiver is not working使用广播接收器重新启动后重新启动服务不起作用
【发布时间】:2018-07-14 09:22:40
【问题描述】:

我想在设备重启后自动启动我的服务,如本网站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>  

【问题讨论】:

    标签: android service broadcastreceiver reboot


    【解决方案1】:

    如果你的目标是 26 以上的 android 版本,那么你必须使用 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() 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      相关资源
      最近更新 更多