1

I want to start my application when phone startup

I just follow tutorial from here but it doesn't work in my device. Please see my method:

public class MyStartUpReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent ii = new Intent();
        ii.setAction("com.sat.servicetrack");
        context.startService(ii);
    }


}

and this is my manifest.xml

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ServiceTrack"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<receiver android:name=".MyStartupReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" >
    <intent-filter>
        <action android:name="com.sat.servicetrack" />
    </intent-filter>
</service>
</application>

Am I missing anything?

Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
akubabas
  • 473
  • 5
  • 13
  • 28

4 Answers4

2

I've done something similiar, but I was starting activity. Here is how I done it:

In Manifest:

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

In Java code:

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        PendingIntent i = PendingIntent.getActivity(context, 0, new Intent(
                context,MainActivity.class),
                Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 20000, i);
    }
}

Your code seems to be correct, but try using PendingIntent ;) Hope it helps you

Nick
  • 2,593
  • 3
  • 30
  • 59
Veljko
  • 1,893
  • 6
  • 28
  • 58
  • i have try put your code, i restart my device..but my service still not running..how it's??sorry for bother you :) – akubabas May 16 '12 at 08:09
  • i have got error like this `05-16 15:24:21.766: ERROR/AndroidRuntime(292): Caused by: java.lang.ClassNotFoundException: com.sat.servicetrack.MyStartupReciever in loader dalvik.system.PathClassLoader[/data/app/com.sat.servicetrack-1.apk] ` what it is?? @Veljko – akubabas May 16 '12 at 08:29
  • try to start some dummy activity, which starts your service. instead of running service immediatly. – Veljko May 16 '12 at 08:30
  • i have got error like this 05-16 15:24:21.766: ERROR/AndroidRuntime(292): Caused by: java.lang.ClassNotFoundException: com.sat.servicetrack.MyStartupReciever in loader dalvik.system.PathClassLoader[/data/app/com.sat.servicetrack-1.apk] what it is?? @Veljko – akubabas May 16 '12 at 08:58
  • have you just copied my code? mine receiver is called BootUpReceiver and yours MyStartUpReciever. Check that...you have made some typo for sure. – Veljko May 16 '12 at 09:20
2

try like this....

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent i = new Intent(context, BootingActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

in manifest file...

 <receiver android:name=".BroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </receiver>
user1213202
  • 1,305
  • 11
  • 23
0

You are not calling the Service.

Code like this.

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

Click Here to know how to start service from broadcast receiver

0
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            context.startService(new Intent(context, BootService.class));
        }
    }

and next you should implements BootService class extended Service

Joonsung Kim
  • 246
  • 1
  • 3
  • 15