Android Repeating Alarm invoking a service

2019-07-20 20:31发布

I am trying to send GPS coordinates to server in every 1 minute interval through a background service . It invokes the service for thee first time but it doesn't repeat it .

Code for setting the alarm:

Intent gpsIntent = new Intent(CheckInActivity.this, GoogleMapService.class);
                    gpsPendingIntent = PendingIntent.getService(CheckInActivity.this, 0, gpsIntent, 0);
                    AlarmManager alarmManager_gps = (AlarmManager)getSystemService(ALARM_SERVICE);

                    Calendar calendar_gps = Calendar.getInstance();
                    calendar_gps.add(Calendar.SECOND, 20);
                    alarmManager_gps.setRepeating(AlarmManager.RTC_WAKEUP, calendar_gps.getTimeInMillis(), TimeUnit.MINUTES.toMillis(1), gpsPendingIntent);

Service

    public class GoogleMapService extends Service {

private boolean gps_enabled = false;
private boolean network_enabled = false;
private static Location sLocation;   
private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
private CallBack callback;

@Override
public void onCreate() {

}

@Override
public IBinder onBind(Intent intent) {

    Log.w(null,"GPS Service called");
    locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    try {
        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    }
    if (network_enabled) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
    }

    callback = new CallBack() {

        public void onResult(Boolean result) {
            // TODO Auto-generated method stub

        }

        public void onProgress() {
            // TODO Auto-generated method stub

        }
    };

    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {       
    super.onStart(intent, startId);
}

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}

class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        if (location != null) {
            // This needs to stop getting the location data and save the battery power.
            locManager.removeUpdates(locListener);
            sLocation = location;
            Model.coordinates = location.getLatitude() + "," + location.getLongitude();
            try{
                SendGPSCoordinatesOperation task = new SendGPSCoordinatesOperation(callback);
                task.execute("");
                Log.w(null,"Sent");
            }
            catch(Exception e){
                Log.w(null,"Couldn't be sent");
            }
            Log.w(null,"The coordinates are"+Model.coordinates);
        }
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

}

EDIT : AndroidManifest code

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-20 20:44
gpsPendingIntent = PendingIntent.getService(CheckInActivity.this, 0, gpsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
查看更多
Juvenile、少年°
3楼-- · 2019-07-20 20:52

Use this...

private void setLocationSendingAlarm() {

        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplicationContext(), GoogleMapService.class);
        intent.putExtra("locationSendingAlarm", true);
        PendingIntent   pendingIntent = PendingIntent.getService(this, 987654321, intent,0);
        try {
            alarmManager.cancel(pendingIntent);
        } catch (Exception e) {

        }
        int timeForAlarm=60000;


        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent);
    }
查看更多
登录 后发表回答