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

一段时间后,Android服务停止运行,请帮助,第1张

概述我在forever循环中有一项服务,但随着时间的流逝,它要么死掉,要么循环结束.我已经有一段时间了,任何建议或建议都将不胜感激.谢谢!这是启动服务的方式IntentmyIntent=newIntent(this,MyService.class);pendingIntent=PendingIntent.getService(this,0,my

我在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<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;    }

解决方法:

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

总结

以上是内存溢出为你收集整理的一段时间后,Android服务停止运行,请帮助全部内容,希望文章能够帮你解决一段时间后,Android服务停止运行,请帮助所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1083672.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存