나는 경보 서비스를 사용했다. 1 분마다 실행되도록 알람을 구현했습니다. 따라서 앱이 종료 된 후에도 브로드 캐스트 리시버 onReceive 메소드가 호출됩니다. 이 onReceive 메서드 내부에 타이머를 구현하고 싶습니다. 위치 추적을 시작하고 20 초 후에 위치 추적을 중지해야합니다.앱이 종료 된 후에도 알람 수신기 내부에서 실행되는 타이머를 구현하는 방법은 무엇입니까?
나는 아래
TimerTask
Handler
Local Broadcast Receiver
을 시도하지만 응용 프로그램이 종료되면 실행 위의 모든 할 수 없다.
알람 수신기 안에 20 초 동안 기다리는 타이머를 구현하고 싶습니다.
앱을 종료 할 때 어떻게해야합니까?
내 AlaramReceiver onReceive 방법 :
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mSharedPrefManager = SharedPrefManager.getInstance(mContext);
mAppUtilInstance.logDebugMessage(TAG, "Track interval alarm receiver.");
// // Check the preference having any current activity
if (mSharedPrefManager.checkForCurrentActivityPref()) {
userActivity = mSharedPrefManager.getCurrentUserActivityPref();
if (mSharedPrefManager.getIsUserMovedPref()) {
// User MOVED
// Call the location service
LocationUpdateTimer locationUpdateTimer = new LocationUpdateTimer(
mContext, userActivity, true);
locationUpdateTimer.initialize();
mSharedPrefManager.setIsUserMovedPref(false);
} else {
// User not MOVED in between the track interval period. He is
// IDLE
// Check whether the location information is returned by the
// google API
if (mSharedPrefManager.checkForLocationEntityPref()
&& mSharedPrefManager.getLocationEntityPref()
.getLatitude().length() > 0) {
// Send the packet information to the Fleetilla server
StoreAndSendLocationInformation storeAndSendLocationInformation = new StoreAndSendLocationInformation(
mContext,
mSharedPrefManager.getLocationEntityPref(),
userActivity);
storeAndSendLocationInformation.storeAndSend();
} else {
// If the location information is not available
mAppUtilInstance
.logDebugMessage(TAG,
"Location information is not generated to store and send.");
}
}
}
}
위치 업데이트 타이머 클래스하십시오 PendingIntent와
/**
* LocationUpdateTimer Constructor
*/
public LocationUpdateTimer(Context context, String userActivity,
boolean isTosend) {
// Save the context
mContext = context;
mUserCurrentActivity = userActivity;
isToSendLocationInfo = isTosend;
}
/**
* To start the location reporting
*/
private void startLocationReporting() {
if (mLocationProviderStatusListener != null) {
mLocationProviderStatusListener
.requestLocationProvidersToUpdateStatus(mConstants.EMPTY_STRING);
scheduleTimerTask();
}
}
/**
* To schedule the 20 seconds timer task to get the best location
* information and send it to Fleetilla server.
*/
private void scheduleTimerTask() {
bestGPSInfoTimerHandler = new Handler();
bestGPSInfoTimerHandler.postDelayed(bestGPSInfoRunnable,
Constants.TIMER_TASK_DELAY);
mAppUtilInstance.logDebugMessage(TAG,
"20 Sec Location Update TimerTask Scheduled");
}
/**
* To cancel the timer tack which was scheduled for 30sec location update
*/
private void cancelTimerTask() {
if (bestGPSInfoTimerHandler != null) {
mAppUtilInstance.logDebugMessage(TAG, "20 sec TimerTask canceled");
bestGPSInfoTimerHandler.removeCallbacks(bestGPSInfoRunnable);
bestGPSInfoTimerHandler = null;
}
}
/**
* A runnable will be called after the 20 sec time interval
*/
Runnable bestGPSInfoRunnable = new Runnable() {
@Override
public void run() {
// Called after 20 seconds
mAppUtilInstance.logDebugMessage(TAG,
"TimerTask running after 20 sec interval.");
stopLocationReporting();
cancelTimerTask();
if (isToSendLocationInfo) {
// Check whether the location information is returned by the
// google api
if (mSharedPrefManager.checkForLocationEntityPref()
&& mSharedPrefManager.getLocationEntityPref()
.getLatitude().length() > 0) {
// Send the packet information to the server
StoreAndSendLocationInformation storeAndSendLocationInformation = new StoreAndSendLocationInformation(
mContext,
mSharedPrefManager.getLocationEntityPref(),
mUserCurrentActivity);
storeAndSendLocationInformation.storeAndSend();
} else {
// If the location information is not available
mAppUtilInstance
.logDebugMessage(TAG,
"Location information is not generated to store and send.");
mAppUtilInstance
.broadcastStatusMessage(
mContext,
Constants.STATUS_MSG_127
+ "Location information is not generated to store and send.",
Constants.STATUS_CODE_127);
}
}
}
};
'onReceive' 메소드에 대한 코드를 보여줄 수 있습니까? –
'PendingIntent.getBroadcast' 메소드에서 다른 'requestCode'를 사용하여 여러 개의 알람을 시작할 수 있습니까? –
이미 다른 요청 코드로 2 개의 다른 알람을 구현했습니다. 나는이 게시물과 함께 내 onreceive 코드를 업데이 트됩니다. –