I am new to android. i develop an application in which i calculate longitude & latitude of my current location and updating date & time at particular interval of time using handler for it & it map the location on Google map by getting data from this application, this application works accurately with updates but one problem is that this application is not working when mobile phone is locked.
- I want to run this application in background & it keep running until & unless then mobile phone is switched off.
This application also start giving update when i reopen application after going back from this application. - I want to display that date & time that is last updated. not restart application & not update date & time from current opening time. it continuously working in background & display the activity that is working in background.
Please help…
I’ll be very thankful
My code of main activity is here…
package com.ETrack;
public class ETrackActivity extends Activity {
public void acquire()
{ final String TAG = null;
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(
getApplicationContext().POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
wl.release();
}
private static final List<? extends NameValuePair> nameValuePairs = null;
/** Called when the activity is first created. */
static TelephonyManager tm;
TextView GPS_Long_txtbx;
TextView GPS_Lat_txtbx;
static TextView last_update_time_txtbx;
TextView onfig_txtbx;
static String pswd_auth= "13579", server_host="helpdesk.cispl.com";
static String interval = "120";
String Saveinterval = "interval.txt";
String FILENAME3 = "savetime";
String Savetime = "savetime.txt";
Handler h = new Handler();
Runnable run = new Runnable()
{
@Override
public void run() {
MyLocation loc = new MyLocation(getBaseContext());
TextView longitude_view = (TextView) findViewById(R.id.GPS_Long_txta);
((TextView) longitude_view)
.setText(" " + String.valueOf(loc.gps_lon));
TextView latitude_view = (TextView) findViewById(R.id.GPS_Lat_txta);
((TextView) latitude_view).setText(" " + String.valueOf(loc.gps_lat));
long delaytime =(record * 1000);
last_update_time_txtbx = (TextView) findViewById(R.id.last_update_time_txt);
last_update_time_txtbx.setText(" " + String.valueOf(java.text.DateFormat.getDateTimeInstance()
.format(Calendar.getInstance().getTime())));
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String response = CallWebService(tm.getDeviceId(), loc.gps_lon, loc.gps_lat);
h.postDelayed(this, delaytime);
}
};
public void Saveinterval(String Saveinterval) {
try {
File dir = getFilesDir();
File file = new File(dir, Saveinterval);
if (file.exists())
file.delete();
FileOutputStream fos = openFileOutput(Saveinterval,
Context.MODE_PRIVATE);
fos.write(Saveinterval.getBytes());
fos.close();
} catch (Exception ex) {
ex.getMessage();
}
}
public int RestoreRecord0()
{
try
{
File dir = getFilesDir();
File file = new File(dir, Saveinterval);
if (!file.exists()) {
return Integer.parseInt(interval);
}
FileInputStream fileIS = new FileInputStream(file);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
String data = " ";
// just reading each line and pass it on the debugger
while ((readString = buf.readLine()) != null)
{
data += readString;
}
return Integer.parseInt(data);
}
catch (Exception ex) {
ex.getMessage();
}
return Integer.parseInt(interval);
}
int record = RestoreRecord0();
private static int activities = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
last_update_time_txtbx = (TextView) findViewById(R.id.last_update_time_txt);
last_update_time_txtbx.setText(" "+ String.valueOf(java.text.DateFormat.getDateTimeInstance()
.format(Calendar.getInstance().getTime())));
MyLocation loc = new MyLocation(this.getApplicationContext());
TextView longitude_view = (TextView)findViewById(R.id.GPS_Long_txta);
((TextView) longitude_view).setText(" "+String.valueOf(loc.gps_lon));
TextView latitude_view = (TextView)findViewById(R.id.GPS_Lat_txta);
((TextView) latitude_view).setText(" "+String.valueOf(loc.gps_lat));
//refresh_location(GPS_Long_txtbx,GPS_lat_txtbx);
final Button btnSkip = (Button) findViewById(R.id.btnRegister);
// btnSkip.setTypeface(face);
btnSkip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click EditText et
Intent i = new Intent(ETrackActivity.this, Configuration.class);
//i.putExtra("Skip", "true");
// i.putExtra("Location", txtEmail.getText());
startActivity(i);
}
});
// last_update_time_txtbx = (TextView)findViewById(R.id.last_update_time_txt);
// last_update_time_txtbx.setText(" "+ String.valueOf(java.text.DateFormat.getDateTimeInstance()
// .format(Calendar.getInstance().getTime())));
// in this response variable it contains
String response = CallWebService(tm.getDeviceId(), loc.gps_lon, loc.gps_lat);
long delaytime =(record * 1000);
h.postDelayed(run, delaytime);
}
// public void refresh_location(View longitude_view, View latitude_view, View last_update_time_txtbx)
public void refresh_location(View longitude_view, View latitude_view)
{
longitude_view = (TextView)findViewById(R.id.GPS_Long_txta);
((TextView) longitude_view).setText(" "+String.valueOf(LocationService.GPS_Long));
latitude_view = (TextView)findViewById(R.id.GPS_Lat_txta);
((TextView) latitude_view).setText(" "+String.valueOf(LocationService.GPS_Lat));
// last_update_time_txtbx = (TextView)findViewById(R.id.last_update_time_txt);
// ((TextView) last_update_time_txtbx).setText(""+String.valueOf(java.text.DateFormat.getDateTimeInstance()
// .format(Calendar.getInstance().getTime())));
}
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onClick(View view)
{
startActivity(new Intent(ETrackActivity.this,Configuration.class));
}
public String CallWebService(String imei, double longt, double lat) {
String response = new String();
String str = new String();
try {
String url = "http://helpdesk.cispl.com/etracking/track.php?imei="+imei+"&gps_lat="+ Double.toString(lat) +"&gps_lon="+Double.toString(longt);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI webservice = new URI(url);
request.setURI(webservice);
HttpResponse httpResponse = client.execute(request);
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity != null) {
response = EntityUtils.toString(responseEntity);
}
} catch (Exception e1){
e1.printStackTrace();
}
return response;
}
}
please give me solution…..
To run the application in background you need to use Service
Tutorials on using services: here and here and this