Java – Automatically starts Android services

Automatically starts Android services… here is a solution to the problem.

Automatically starts Android services

How to automatically start the service in Android 3.x, the test tablet is Samsung Galaxy 10.1. My code is for Noname tablets with Android 2.2.1, and the code also works for Android emulators for Android version 3.x

Code:

StartAtBootService.java
package test.autostart;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StartAtBootService extends Service 
{
        public IBinder onBind(Intent intent)
        {
            return null;
        }

@Override
        public void onCreate() 
        {
            Log.v("StartServiceAtBoot", "onCreate");
        }

@Override
        public int onStartCommand(Intent intent, int flags, int startId) 
        {
            Log.v("StartServiceAtBoot", "onStartCommand()");          
            return START_STICKY;
        }

@Override
        public void onDestroy() 
        {
            Log.v("StartServiceAtBoot", "onDestroy");
        }
}

StartAtBootServiceReciver.java
package test.autostart;

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

public class StartAtBootServiceReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("test.autostart.StartAtBootService");
            context.startService(i);
        }
    }
}

list

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="StartAtBootService">
            <intent-filter>
                <action android:name="test.autostart.StartAtBootService">
                </action>
            </intent-filter>
        </service>
        <receiver android:name="StartAtBootServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                </action>
                <category android:name="android.intent.category.HOME">
                </category>
            </intent-filter>
        </receiver>
    </application>
</manifest>

Solution

This is an SD card issue and Eclipse installs new apps on my SD card by default on my Samsung Galaxy 10.1. To solve this problem, I need to add android:installLocation=”internalOnly” to the list.

New list:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.autostart"
      android:versionCode="1"
      android:versionName="1.0" android:installLocation="internalOnly">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="StartAtBootService">
            <intent-filter>
                <action android:name="test.autostart.StartAtBootService">
                </action>
            </intent-filter>
        </service>
        <receiver android:name="StartAtBootServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                </action>
                <category android:name="android.intent.category.HOME">
                </category>
            </intent-filter>
        </receiver>
    </application>
</manifest>

I hope this helps the people of the future.

Related Problems and Solutions