Question

im trying to start an application on device boot.

my code is as follow

1- firstly the main class which contain a background thread (asynctask) that send data to a mysql database.

2- Service class

package com.seven.ex.helper;

import com.seven.ex.AndroidGPSTrackingActivity;

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

public class service extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
    return null;
}
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid)
{
    Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intents);
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
}
}    

3- BootUpReceiver class

package com.seven.ex.helper;

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

public class BootUpReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
    Intent intent = new Intent(arg0,service.class);
    arg0.startService(intent);
    Log.i("Autostart", "started");
}

}

4- the android manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

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

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




   <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <service android:name=".service" 
              android:label="@string/app_name"
              >
    </service>

    <receiver android:enabled="true" android:name=".BootUpReceiver">

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

    </application>


   </manifest>

The main method is working correctly before i tried to start it on boot. Also the app still working after i ignore the android error on boot (The App Closed)

in the log cat am getting a class not found exception

can you please help ?

Was it helpful?

Solution 2

In your manifest you have assigned package: com.example.gpstracking. When u then define .BootUpReceiver. The system should expect to locate the class at com.example.gpstracking.BootUpReceiver.

Please try and change the package .BootUpReceiver to the full path com.seven.ex.helper.BootUpReceiver. The same goes for AndroidGPSTrackingActivity as this should be com.seven.ex.AndroidGPSTrackingActivity as far as I can tell.

OTHER TIPS

Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {

        //do your stuff here

    return Service.START_NOT_STICKY;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top