杀死进程时,服务OnStartCommand再次调用

时间:2014-06-19 02:03:13

标签: android service android-service scheduled-tasks

我开发的应用程序具有向服务器发送呼叫的重复任务,并检查数据是否可用于当前日期。我已经成功实现了这个,但是我有一个问题,当我杀死进程时它再次调用service。我通过Logcat对此进行了监控,或者即使标志不是假的,也会生成PUSH通知(根据我的逻辑)。为什么,因为我使用*START_STICKY*

我也使用了START_REDELIVER_INTENT 标志,它解决了这个问题,但是在我的手机的TaskManager中,当我杀死进程时,我的服务状态在 RESTART 上。该服务是否仍在运行或处于 RESTART 状态?

我希望我的服务在没有 RESTART 的情况下全天候运行。要么我杀了这个过程(如微信,Whatsapp,Viber等)。

有人可以理解我做错了什么吗?或者给我正确的方向。应该感谢帮助。谢谢

服务

    public int onStartCommand(Intent intent, int flags, final int startId) {
         // Log.e("LocalService", "Received start id " + startId + ": " + intent);
            Log.e("AlertService", "Alert-----Service Created");
            // TODO Auto-generated method stub

            ScheduledExecutorService scheduler =
                    Executors.newSingleThreadScheduledExecutor();

                scheduler.scheduleAtFixedRate
                      (new Runnable() {
                         public void run() {
                            // call service
                             DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
                                currentDate = df.format(Calendar.getInstance().getTime());
                                String[] separated_currentDate = currentDate.split("-");
                                int actual_date = Integer.parseInt(separated_currentDate[0]);


                                Log.e("CurrentDate of my Device", currentDate); 
                                Log.e("ad",""+actual_date);

                                if(flag == false)
                                {
                                    Log.e("Flag-Status", "FALSE");
                                    try {
                                        savingDateinPref(currentDate);
                                        isInternetPresent = cm.isConnected();
                                        if(isInternetPresent)
                                        {
                                            Log.e("Flag-Status", "Calling Service");
                                            new DoInBackground().execute(currentDate);
                    //                      currentDate = null;
                                        }

                                    } catch (Exception e) {
                                        // TODO: handle exception
                                        e.printStackTrace();
                                    }

                                }

                                String val = prefs.getString("TAG_KEY", "defvalue");
                                String[] separated_val = val.split("-");
                                int pref_date = Integer.parseInt(separated_val[0]);
                                Log.e("pd", "" +pref_date);

                                if( (pref_date != actual_date) || ( pref_date < actual_date) || (pref_date > actual_date) ) 
                                {
                                flag = false;
                                Log.e("Flag-Status", "FALSE/bcoz date has been changed");
//                              String empty = null;
//                              savingDateinPref(empty);
                                }

                         }
                      }, 0, 5, TimeUnit.SECONDS);

            return START_REDELIVER_INTENT;

    }

0 个答案:

没有答案
相关问题