I have problem with my Android application. I need an application that will send in background GPS location every 10 minutes to the server (here I have WCF service that work). Application needs to send data even if it is closed. So I created a service (for test purposes) that sends to me time on server. But when I insert my code for getting GPS location in my service everything fails (stopped unexpectedly).
I have reed that you need to put some receiver or something but nothing so far works. So I will be very pleased if someone can help me.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biromatik.GPS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ETMGPSServiceActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".MyApplication" >
<service
android:enabled="true"
android:name=".MonitorService" >
</service>
<receiver
android:enabled="true"
android:name=".LocationReceiver" >
<intent-filter >
<action android:name="com.biromatik.intent.action.LOCATION" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
I've done this in my app to do the same thing you asked.
In my service I added this:
NOTE: See that I use MyApplication.getAppContext(); that's because I'm using my own Application class to add the context. And then I have a BroadcastReceiver with this:
the Application class:
Also in your manifest you need to add both:
Method to detect if the service is running I do this on my first Activity:
Obviously you need to control the SharedPreferences (settings) to set the locationService variable to true/false if the service is running (onStart) or not (onDestroy).