0

My English is poor. I cannot start an android service in a boot time and I do not know the problem. I was trying example codes, but without success. Can somebody send me a project in Java that runs? Other code works for other people but on my tablet smartphone emulator it does not work. Does a problem exist in android 4.0?

This is my code:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.service22"
    android:versionCode="1"
    android:versionName="1.0" 
    android:installLocation="internalOnly"
    >

    <supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
            <service android:name="com.example.MyService">
                <intent-filter>
                    <action android:name="com.example.MyService">
                </action>
            </intent-filter>
        </service>
        <receiver android:name="com.example.MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                </action>
                <category android:name="android.intent.category.HOME">
                </category>
            </intent-filter>
        </receiver>
    </application>



</manifest>



    public class MyService extends Service 
    {

       private static final     String LOG_TAG = "::Monitor";


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(LOG_TAG, "Service created.");
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);

        for (int i = 0 ; i < 20 ; i++)
        {
            mensaje();
        }



        Log.e(LOG_TAG, "Service started.");
    }
    @Override
    public void onDestroy() 
    {
           super.onDestroy();
           Log.e(LOG_TAG, "Service destroyed.");
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        Log.e(LOG_TAG, "Service bind.");
        return null;
    }



   public void mensaje()
   {
       Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show();
   }

}



     public class MyReceiver extends BroadcastReceiver 
{
    public MyReceiver() 
    {
    }

    String LOG_TAG = "::StartAtBootServiceReceiver";

    @Override
    public void onReceive(Context context, Intent intent) 
    {

        Log.e(LOG_TAG, "onReceive:");
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("com.example.MyService");
            context.startService(i);
        }



    }

}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
TSW1985
  • 1,179
  • 2
  • 10
  • 13
  • possible duplicate of [Start Android Service on boot time Android 4.0](http://stackoverflow.com/questions/11640142/start-android-service-on-boot-time-android-4-0) – CommonsWare Jul 24 '12 at 23:44
  • possible duplicate of [Trying to start a service on boot on Android](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android) – user3439968 Sep 24 '14 at 21:17

3 Answers3

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

public class OnBootReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d("OnBootReceiver", "Hi, Mom!");
  }
}

and manifest file

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application android:icon="@drawable/cw"
               android:label="@string/app_name">
    <receiver android:name=".OnBootReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>
  </application>
Dawid Sajdak
  • 3,064
  • 2
  • 23
  • 37
  • It's imposible. It does not work, I had do your solutions, but not work. Somebody can send to me a android proyect whit this? Email : gabrielgg85@gmail.com , please I'm frustated. Thanks. – TSW1985 Jul 25 '12 at 10:06
  • 1
    @user1549952: "It does not work" -- yes, it does. However, on Android 3.1+, something must manually run one of your components before any `BroadcastReceiver` will work. That is why [the full sample project from which these code snippets were taken](https://github.com/commonsguy/cw-omnibus/tree/master/SystemEvents/OnBoot) has an activity for the user to launch. Then, your service will get control on all subsequent reboots, or until the user presses Force Stop in the Settings app (in which case, your app needs a manual component launch again before receivers work). – CommonsWare Jul 26 '12 at 13:14
0

I think:

Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);

is not enough,you must set intent class name like that:

Intent i = new Intent();
i.setClassName("com.example", "com.example.MyService");
i.setAction("com.example.MyService");
context.startService(i);

pls try that.

enjoy-writing
  • 520
  • 3
  • 4
0

IMPORTANT: Android OS 3.1+ remains ignorent about your broadcast receivers in following cases: 1.User has never started the application explicitly at least once.
2.User has "force closed" the application.

Issue has nothing to do to your implementation. This is a potential security hole in Android that Google has closed :)

tomurka
  • 851
  • 1
  • 7
  • 16