简体   繁体   中英

Trouble starting my Android service on boot

I am having trouble starting my service on boot.

I have a broadcast receiver that should get called whenever the device boots up (it isn't) , and it starts my service. Unfortunately, the service isn't starting!

I have looked at this page, read every answer, and followed every step...but it is still not working. I want to start my service whenever the phone restarts/powers on.


package curlybrace.ruchir.myApp;

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

public class BootUp extends BroadcastReceiver {

    private static final String TAG = "myTag";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        Log.v(TAG, "Hooray! Received boot! :) "); //Sadly I am not getting this message

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



    }
}

But my service still wont start. Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="curlybrace.ruchir.myApp"
    android:installLocation="internalOnly">

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

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

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


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

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

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




        <receiver
            android:name=".BootUp"
            >

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

        </receiver>
    </application>

</manifest>

I have been stuck on this for the last three days, and I would really appreciate your help. Why is my service not starting on boot?

Here's how I define my auto start receiver:

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

Notice the android.intent.action.BOOT_COMPLETED

Your BroadcastReceiver doesn't get called because the action of the broadcast is no android.intent.action.BOOT_COMPLETED and not RECIEVE_BOOT_COMPLETED .

If you fix that your Receiver should work.

Your code to start the service looks fine.

android:installLocation="internalOnly"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM