2017-02-02 75 views
3

当我读取无论您在警报管理器中定义的时间段在sdk 21上面每隔1分钟发生一次。我正在运行一项服务,我希望我的方法执行每5分钟后。这是我的代码做了运行方式每隔5分钟在服务上面sdk 21

public class Servicebackground extends Service { 
@Nullable 
@Override 

public IBinder onBind(Intent intent) { 
    System.out.println("service started in onBind"); 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Intent notificationIntent = newIntent(this,MyLocationListener.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, pendingIntent); 


    System.out.println("service started in on create"); 




    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    System.out.println("service stopped"); 

} 


@Override 
public void onCreate() { 


     return; 
    } 


    public class MyLocationListener extends BroadcastReceiver implements LocationListener { 

    @Override 
    public void onReceive(Context context, Intent intent) { 


     Intent ii = new Intent(context, Servicebackground.class); 
     startService(ii); 
     } 

在课堂上我打电话我已经做

startService(new Intent(this, Servicebackground.class)); 

但即使我已经定义的时间为5分钟,方法每一分钟后运行服务。如果你正在使用报警管理器,无论你什么时候定义该方法,每隔1分钟后,我会读到sdk 21之后。

请告诉我是否有任何错误。或者,请以任何其他方式向我建议,即使应用程序被杀死,并且每5分钟执行一次该方法,我也能够在后台运行该服务。如果还有其他可能的方法,请提出建议。

回答

-1

alaram setReating not work api 19因此您使用处理程序手动重复过程。参考AlarmManager fires alarms at wrong time

Handler handler; 
    Timer timer; 
    TimerTask doAsynchronousTask; 
    public AlarmManager alarm; 

的onResume

Intent intent = new Intent(getActivity(),Services.class); 

final Calendar cal = Calendar.getInstance(); 
     pintent = PendingIntent.getService(getActivity(), 0, intent, 0); 
     alarm = (AlarmManager) getActivity().getSystemService(
       Context.ALARM_SERVICE); 


      if (Const.DEVICE_API_INT < Const.ANROID_API_LOILLIPOP) { 
       alarm.setRepeating(AlarmManager.RTC_WAKEUP, 
         cal.getTimeInMillis(), 3 * 1000, pintent); 
      } else { 

       handler = new Handler(); 
       timer = new Timer(); 
       doAsynchronousTask = new TimerTask() { 

        @Override 
        public void run() { 
         handler.post(new Runnable() { 

          @SuppressLint("NewApi") 
          public void run() { 
           try { 
            alarm.setExact(AlarmManager.RTC_WAKEUP, 
              cal.getTimeInMillis(), pintent); 

           } catch (Exception e) { // TODO Auto-generated 
                 // catch 
           } 
          } 
         }); 
        } 
       }; 
       timer.schedule(doAsynchronousTask, 0, 3000); 

的onCreate

receiver = new BroadcastReceiver() { 

       @Override 
       public void onReceive(Context context, Intent intent) { 

        Bundle bundle = intent.getExtras(); 
        if (bundle != null) { 

         int resultCode = bundle 
           .getInt(Services.RESULT); 
         if (resultCode == getActivity().RESULT_OK) { 
          trainingStatus = bundle 
            .getString(Services.RESPONSE); 
          if (Status.equalsIgnoreCase("finished")) { 

           if (intent != null) { 
            getActivity().stopService(intent); 
           } 
           alarm.cancel(pintent); 
          } else { 

          } 

        } 
       } 
      }; 

服务

@Override 
    protected void onHandleIntent(Intent intent) { 

     Log.e("TAG", " Training services start"); 

     Bundle extras = intent.getExtras(); 
publishResults(trainingStatus, resultInt); 
} 
private void publishResults(String response, int result) { 
     Intent intent = new Intent(FILTER); 
     intent.putExtra(RESPONSE, response); 
     intent.putExtra(RESULT, result); 
     sendBroadcast(intent); 
    } 
+0

最好解释一下你的答案,而不是简单地在用户上转载一大堆未注释的代码。 – Kuffs

+0

Y投票最新怎么了? – RDY

+0

请解释你的代码。 –

0

使用处理器和TimerTask

Timer timer; 
TimerTask task; 
private Handler handler; 
@Override 
protected void onCreate() { 
    handler = new Handler(); 
    if (Build.VERSION.SDK_INT > 21) { 
    startTimerTask(); 
    } 
} 

public void startTimerTask() { 

    Log.w("--> ", "Start timer call"); 
    timer = new Timer(); 
    task = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        executeMethod(); 
       } 
      }); 
     } 
    }; 
    timer.schedule(task, 0, 60000 * 5); 
} 

private void executeMethod() { 
    // Do what you want 
} 
+0

即使应用程序将被杀死,计时器任务是否会触发该功能? –

+0

是的,如果有写入服务,那么肯定会引发.... @VipinNU –