1
我是android新手。我開發了一個應用程序,其中我計算我的當前位置的經度&緯度和更新日期&使用處理程序的特定時間間隔&它通過從此應用程序獲取數據來映射Google地圖上的位置,此應用程序可以與更新但是一個問題是,當手機被鎖定時,這個應用程序不工作。不想在每次重新打開時重新啓動應用程序
- 我想在後臺運行&該應用中它繼續運行,直到&除非當時的手機關機。 此應用程序也開始給予更新,當我從這個應用程序返回後重新打開應用程序。
- 我想顯示該日期&上次更新的時間。不重新啓動應用程序&不更新日期&從目前的開放時間。它在後臺連續工作&顯示在後臺工作的活動。
請幫助... 我會很感激
我的主要活動的代碼是在這裏...
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;
}
}
請給我的解決方案.....
我使用暫停和恢復服務。但不起作用。 – bushra
你能舉個例子說明如何使用它。 – bushra