0

I am making an android app to log current user location to my server every few hour. So far I have made a class to give me current user location and i have also been able to send location data to web. I have also been able to register a service in my app to run, even my app has been cleared from background. Now I want to integrate all these, but cant seem to integrate it.

My main activity is

public class MainPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainpage);

    Intent i= new Intent(MainPage.this, RunnerService.class);
    this.startService(i);
}
}

and my service is:

public class RunnerService extends Service {


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Log.d("SahiyogiHaat", "Service created");

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.d("SahiyogiHaat", "Service started");
      return Service.START_STICKY;

}
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

So where should I make object of my location tracker class to run the service and access user location every 1 hour. In addition to this I tried to implement on reboot broadcast receiver as follows and implement the service from there when user switches on his mobile:

public class MyReceiver  extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent i= new Intent(context, RunnerService.class);
    // potentially add data to the intent
    context.startService(i);
   }
}

but the service doest start but it says "unfortunately application stopped working", which mean the broadcast has been received but there is some problem. My manifest file is as below:

Manifest

ashwin shrestha
  • 107
  • 1
  • 13
  • post the [stacktrace of the exception](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Blackbelt Oct 11 '15 at 15:58
  • on reboot i am not being able to catch the exception – ashwin shrestha Oct 11 '15 at 16:46
  • The exception will always be logged in logcat. Any time after the app crashes, just connect your device to your machine and run `adb logcat` - it will display all the events since the device started and include the stacktrace of your crash. – adelphus Oct 11 '15 at 23:24

1 Answers1

0

If you want to get user's location every hour, you need to use one of the location listener mechanisms (either the native Android location API or the Google Location Services API). You can then register with the location manager for location callbacks every hour. In this case you should pass a PendingIntent to the location manager, so that it either starts your Service every hour or triggers a BroadcastReceiver every hour with the GPS location data.

Also, Android cannot start your BroadcastReceiver on device boot. To fix this, remove

android:exported="true"

from the manifest declaration for <receiver>

David Wasser
  • 93,459
  • 16
  • 209
  • 274