Android 重复闹钟调用服务

标签 android android-service

我正在尝试通过后台服务每隔 1 分钟将 GPS 坐标发送到服务器。它会为您第一次调用该服务,但不会重复调用。

闹钟设置代码:

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);

服务

    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

    }
}

}

编辑:AndroidManifest 代码

最佳答案

使用这个...

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);
    }

关于Android 重复闹钟调用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160377/

相关文章:

android - 如何使用 Google Play 服务 API 客户端进行 Activity 识别

android - android小部件是否在与其应用程序相同的进程中运行

android - 是否可以连接到 Spotify 的 MediaBrowserService?

android - 防止 Android 服务在解除绑定(bind)后被破坏

android - 如何在 kitkat 4.4 上录制我正在进行的屏幕的视频

java - 在 Gingerbread 中水平滑动后未触发操作栏的 onNavigationItemSelected(int position, long itemId)

android - 使用 TextWatcher 在 EditText 中设置最大字数限制

java - 如何重新格式化时间格式?

android - 想要使用 Activity android 运行和停止服务

安卓:意向服务。在哪里保存文件?