3
我有一個永久循環的服務,但隨着時間的推移,它會死亡或循環結束。我一直在這一段時間,任何意見或建議將非常感激。謝謝!Android服務在一段時間後停止運行請幫助
這裏是服務是如何爲您服務是沒有前景的服務
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try{
//Toast.makeText(getApplicationContext(), " Service Started", Toast.LENGTH_LONG).show();
@SuppressWarnings({ "rawtypes", "unchecked" })
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask() {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Object... objects) {
//Get Username from sql not static value. Static value could die.
String Username = "";
SQLiteDatabase db;
db = openOrCreateDatabase(".db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
Cursor cur =db.query("userdata", null, null, null, null, null, null);
cur.moveToFirst();
while(cur.isAfterLast()==false){
Username = (cur.getString(cur.getColumnIndex("username")));
cur.moveToNext();
}
cur.close();
db.close();
int messagecount = -1;
//START MAIN LOOP TO CHECK NEW MESSAGES
/*
* Counts starts at -1 then gets count then if 1 is
* added to newcount newcount is > than last message count
* notify uesers.
*/
for(;;){
try{
String result = "";
//Shownotify();//WILL LOOP ALL THE TIME: LOOP TEST
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http:GetInfo.php");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("UserName", Username));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!= null){
sb.append(line + "\n");
}
is.close();
result = sb.toString();
JSONArray jArray = new JSONArray(result);
int newcount = 0;
for(int i=0;i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
newcount = json_data.getInt("counter");
}
if(MyGlobalInformation.getStopLoop() == true){
break;
}
if(newcount > messagecount && messagecount != -1){
messagecount = newcount;
Shownotify();
}
else{
messagecount = newcount;
}
}
catch(Exception e){
HomeScreen.setStartedState(false);
}
}
return null;
}
@Override
protected void onPostExecute(Object o) {
HomeScreen.setStartedState(false);
}
};
asyncTask.execute();
}
catch(Exception e){
HomeScreen.setStartedState(false);
}
return START_STICKY;
}
你能告訴我如何在我的代碼上面?謝謝! – user516883 2011-03-05 00:52:09
我目前無法使用編輯器,所以我不能。我可以指出你以前寫過的代碼中的例子。有關startForeground的示例,請查看以突出顯示的行開始的塊:http://code.jakebasile.com/hearing-saver/src/c794b7659584/src/com/jakebasile/android/hearingsaver/RegistrationService.java#cl- 47 使用START_REDELIVER_INTENT也很簡單,只需從onStartCommand中返回它即可,並且該方法將在服務被終止時以相同的意圖重新調用。 – 2011-03-05 05:38:28
鏈接已死亡... – crushman 2014-05-18 14:00:47