Hi friends i am having some problems with my code. I am very new to android programming, actually i am an electronics student. i have searched the forum for an answer to this problem but nothing says precisely what i have to do.i have given the log cat which explains my problem.One more thing When i add a empty constructor it shows an error "The blank final field context may not have been initialized" Please help me.
public class TrackService extends IntentService implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
//boolean uploaded = false;
private Context context= getApplicationContext();
String Lat, Long;
// A request to connect to Location Services
private LocationRequest mLocationRequest;
//List<?> pkgAppsList;
//ArrayList<ArrayList<String>> list;
//Context context;
//ArrayList<String> iteminserted = new ArrayList<String>();
private LocationClient mLocationClient;
// ArrayList<ArrayList<String>> UnUploadedData = new
// ArrayList<ArrayList<String>>();
//@Override
//public IBinder onBind(Intent intent) {
//return null;
//}
//public TrackService() {
//super("TrackService");
//}
public TrackService(Context context) {
super("Trackservice");
this.context = context;
}
@Override
public void onCreate() {
super.onCreate();
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
/*
* Set the update interval
*/
// mLocationRequest
// .setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setInterval(1000 * 60 * 2);// Every 2 minute
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest
.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
mLocationClient = new LocationClient(this, this, this);
}
/*
* Called by Location Services when the request to connect the client
* finishes successfully. At this point, you can request the current
* location or start periodic updates
*/
@Override
public void onConnected(Bundle bundle) {
startPeriodicUpdates();
}
/**
* Report location updates to the UI.
*
* @param location
* The updated location.
*/
@Override
public void onLocationChanged(Location location) {
System.out.println(location.getLatitude() + " "+ location.getLongitude());
Lat = location.getLatitude() + "";
Long = location.getLongitude() + "";
//if (isConnectingToInternet())
new UploadLocationInfo().execute();
// if (uploaded)
// this.stopSelf();
}
/**
* In response to a request to start updates, send a request to Location
* Services
*/
private void startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
/**
* In response to a request to stop updates, send a request to Location
* Services
*/
private void stopPeriodicUpdates() {
mLocationClient.removeLocationUpdates(this);
}
//public boolean isConnectingToInternet() {
//ConnectivityManager connectivity = (ConnectivityManager) this
// .getSystemService(Context.CONNECTIVITY_SERVICE);
//if (connectivity != null) {
//NetworkInfo[] info = connectivity.getAllNetworkInfo();
//if (info != null)
//for (int i = 0; i < info.length; i++)
//if (info[i].getState() == NetworkInfo.State.CONNECTED) {
//return true;
//}
//}
//return false;
//}
public class UploadLocationInfo extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Uploading Application List
//ArrayList<NameValuePair> LOCATION = new ArrayList<NameValuePair>();
//LOCATION.add(new BasicNameValuePair("LAT", Lat));
//LOCATION.add(new BasicNameValuePair("LONG", Long));
//TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext()
// .getSystemService(getApplicationContext().TELEPHONY_SERVICE);
//IMEI = telephonyManager.getDeviceId();
//LOCATION.add(new BasicNameValuePair("IMEI", IMEI));
double x = Double.parseDouble(Long);
double y = Double.parseDouble(Lat);
EditText text1 = (EditText) ((Activity)context).findViewById(R.id.text1);
text1.setText("lat"+y+"Log"+x);
double a = 48.6800000;
double b = 2.2100000;
float[] results = new float[1];
Location.distanceBetween(y, x, b, a, results);
float distanceInMeters = results[0];
boolean isWithin10m = false;
if( distanceInMeters < 20)
{
isWithin10m = true;
}
System.out.println("Uploading New Location");
if(isWithin10m){
try {
//open a web page
} catch (Exception e) {
e.printStackTrace();
}
}else{
text1.setText("nothing to display");
}
return null;
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// If the client is connected
System.out.println("Destroy");
if (mLocationClient.isConnected()) {
stopPeriodicUpdates();
}
// After disconnect() is called, the client is considered "dead".
mLocationClient.disconnect();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
}
The Logcat details ae given below
07-22 11:49:37.419: D/dalvikvm(5525): Late-enabling CheckJNI
07-22 11:49:37.490: D/dalvikvm(5525): newInstance failed: no <init>()
07-22 11:49:37.490: D/AndroidRuntime(5525): Shutting down VM
07-22 11:49:37.490: W/dalvikvm(5525): threadid=1: thread exiting with uncaught exception (group=0x40d1a930)
07-22 11:49:37.490: E/AndroidRuntime(5525): FATAL EXCEPTION: main
07-22 11:49:37.490: E/AndroidRuntime(5525): java.lang.RuntimeException: Unable to instantiate service com.example.mobiletrackerslave.TrackService: java.lang.InstantiationException: can't instantiate class com.example.mobiletrackerslave.TrackService; no empty constructor
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2513)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.app.ActivityThread.access$1600(ActivityThread.java:141)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.os.Looper.loop(Looper.java:137)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 11:49:37.490: E/AndroidRuntime(5525): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 11:49:37.490: E/AndroidRuntime(5525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 11:49:37.490: E/AndroidRuntime(5525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 11:49:37.490: E/AndroidRuntime(5525): at dalvik.system.NativeStart.main(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525): Caused by: java.lang.InstantiationException: can't instantiate class com.example.mobiletrackerslave.TrackService; no empty constructor
07-22 11:49:37.490: E/AndroidRuntime(5525): at java.lang.Class.newInstanceImpl(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525): at java.lang.Class.newInstance(Class.java:1319)
07-22 11:49:37.490: E/AndroidRuntime(5525): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2510)
07-22 11:49:37.490: E/AndroidRuntime(5525): ... 10 more
07-22 11:49:39.892: D/dalvikvm(5525): Debugger has detached; object registry had 1 entries