设备重置时启动服务

时间:2018-04-19 09:58:04

标签: android service

我有广播接收器

public class StartServiceReceiver扩展BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TEST", " start");
    context.startService(new Intent(context, MyService.class));
}

}

和我的清单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".Activity.ListCitiesActivity">
    </activity>
    <activity android:name=".Activity.InfoActivity">
    </activity>
    <activity
        android:name=".Settings.SettingsActivity"
        android:label="@string/title_activity_settings"
        android:theme="@style/Theme.AppCompat.Light">
    </activity>
    <activity android:name=".Activity.DetailActivity"></activity>

    <service android:name=".MyService"/>

    <receiver
        android:name=".StartServiceReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:enabled="true">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>

但是当我启动设备时,服务无法启动 log Log.d(&#34; TEST&#34;,&#34; start&#34;);不显示 我该如何解决?

1 个答案:

答案 0 :(得分:0)

在清单文件中添加此权限:

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

Update1 :尝试添加此

 <receiver
            android:name=".StartServiceReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>