android服务停止运行怎么办,一段时间后,Android服务停止运行,请帮助

我在forever循环中有一项服务,但随着时间的流逝,它要么死掉,要么循环结束.我已经有一段时间了,任何建议或建议都将不胜感激.谢谢!

这是启动服务的方式

Intent myIntent = new Intent(this, MyService.class);

pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 10);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

这是该服务的Onstart命令

@Override

public int onStartCommand(Intent intent, int flags, int startId)

{

try{

//Toast.makeText(getApplicationContext(), " Service Started", Toast.LENGTH_LONG).show();

@SuppressWarnings({ "rawtypes", "unchecked" })

AsyncTask 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 postParameters = new ArrayList();

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

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;

}

解决方法:

您的服务不是前台服务,因此可以随时将其终止.您可以使用Service.startForeground解决问题.您也可以通过从onStartCommand返回START_REDELIVER_INTENT进行修复,然后它将以相同的意图再次调用您的服务.请查阅文档的this part,以获取有关服务生命周期的更多信息.

标签:android,service

来源: https://codeday.me/bug/20191102/1994617.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值