I want a simple service without Binder. Just start the service at boot time and no destroy of the service.
I have build a Service with a location manager. But the service does not start. What ist wrong?
public class WayService extends Service implements LocationListener {
blabla
...
public WayService(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) { //! hinzugefuegt
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
time = location.getTime();
speed = location.getSpeed();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
blabla
...
@Override
public void onLocationChanged(Location location) {
double latitude = getLatitude();
double longitude = getLongitude();
String mlat = String.valueOf(latitude);
String mlon = String.valueOf(longitude);
new PostData().execute(mlat, mlon, mtime);
}
The Manifest looks like this:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".WayService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
</service>
<receiver android:name="WayStartServiceReceiver" >
</receiver>
</application>
And:
public class WayStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WayService.class);
context.startService(service);
}
}
here is my logfile:
01-09 13:35:29.317: D/dalvikvm(452): newInstance failed: no <init>()
01-09 13:35:29.337: D/AndroidRuntime(452): Shutting down VM
01-09 13:35:29.337: W/dalvikvm(452): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-09 13:35:29.367: E/AndroidRuntime(452): FATAL EXCEPTION: main
01-09 13:35:29.367: E/AndroidRuntime(452): java.lang.RuntimeException: Unable to instantiate service de.app.trackmyway.WayService: java.lang.InstantiationException: de.app.trackmyway.WayService
01-09 13:35:29.367: E/AndroidRuntime(452): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.os.Looper.loop(Looper.java:123)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 13:35:29.367: E/AndroidRuntime(452): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:35:29.367: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 13:35:29.367: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 13:35:29.367: E/AndroidRuntime(452): at dalvik.system.NativeStart.main(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452): Caused by: java.lang.InstantiationException: de.app.trackmyway.WayService
01-09 13:35:29.367: E/AndroidRuntime(452): at java.lang.Class.newInstanceImpl(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452): at java.lang.Class.newInstance(Class.java:1409)
01-09 13:35:29.367: E/AndroidRuntime(452): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
01-09 13:35:29.367: E/AndroidRuntime(452): ... 10 more
i hope We cannot create the constructor in Android service. Like we cannot create the constructor in our Activity. we will be having our default override method as follow.
else try to use oncreate in Service class. in the on-create call the method getLocation(); If you will try with the oncreate means surely it will work. you wont get any issue.