小编use*_*018的帖子

我想每15分钟向服务器发布位置更新,即使应用程序没有在前台运行

我希望从Android手机到服务器每隔15分钟到服务器发送一次位置更新.这是服务或报警管理器是最好的选择.

如果我启动服务,我可以启动asynctask将位置发布到服务器.

这是我使用的代码:

    @Override
    public void onStart(Intent intent, int startId) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new MyLocationListener();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                4000, 0, listener);
    }




    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(final Location loc) {
            Log.i("**************************************", "Location changed");
            if (isBetterLocation(loc, previousBestLocation)) {
                loc.getLatitude();
                loc.getLongitude();

//              
//              intent.putExtra("Latitude", loc.getLatitude());
//              intent.putExtra("Longitude", loc.getLongitude());
//              intent.putExtra("Provider", loc.getProvider());
//              
//              sendBroadcast(intent);

            }
        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String provider) { …
Run Code Online (Sandbox Code Playgroud)

service android alarmmanager

5
推荐指数
1
解决办法
4475
查看次数

标签 统计

alarmmanager ×1

android ×1

service ×1