当应用程序关闭时将LocationListener的工作? [英] will locationListener work when application is off?

查看:112
本文介绍了当应用程序关闭时将LocationListener的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送GPS位置到服务器的每5分钟,

甚至当我的应用程序已关闭。

我明白AlertManager可以preform这一点。

会是相同的效率明智的设置与LocationMaanger

minTime = 5分钟
minGPS_delta = 0

在一般的时候你可以使用:

1)View.postDelay();

2)LocationMaanger.setListener()与minTime = 5分钟并minGPS_delta = 0

3)AlertManager

4)定时


解决方案

 进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.location.Location;
进口android.location.LocationManager;
进口android.nfc.Tag;
进口android.os.Bundle;
进口android.os.IBinder;
。进口的Andr​​oid preference preferenceManager。
进口android.provider.Telephony;
进口android.telephony.TelephonyManager;
进口android.util.Log;
进口android.widget.Toast;进口org.apache.http.NameValuePair;
进口org.apache.http.message.BasicNameValuePair;进口java.net.URLEn codeR;
进口java.text.SimpleDateFormat的;
进口的java.util.ArrayList;
进口的java.util.Calendar;
进口java.util.Date;
进口java.util.TimeZone中;公共类为MyService扩展服务
{
    私有静态最后弦乐TAG =BOOMBOOMTESTGPS;
    私人的LocationManager mLocationManager = NULL;
    私有静态最终诠释LOCATION_INTERVAL = 300000;
    私有静态最终浮动LOCATION_DISTANCE = 328.08f;
    串SLAT;
    串sLong;
    串速度;
    字符串盐;
    字符串表准时;
    IMEI串;
    字符串电话;
    字符串位置code;
    串LOCALTIME;    私有类LocationListener的实现android.location.LocationListener {
        位置mLastLocation;        公共LocationListener的(字符串提供商){
            Log.e(TAGLocationListener的+供应商);
            mLastLocation =新位置(供应商);
        }        @覆盖
        公共无效onLocationChanged(地点){
            日历CAL = Calendar.getInstance(TimeZone.getTimeZone(GMT-7:00));
            日期currentLocalTime = cal.getTime();
            SimpleDateFormat的日期=新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS);
            date.setTimeZone(TimeZone.getTimeZone(GMT-7:00));
            LOCALTIME = date.format(currentLocalTime);
            SLAT = Double.toString(location.getLatitude());
            sLong = Double.toString(location.getLongitude());
            速度= Double.toString(location.getSpeed​​());
            盐= Double.toString(location.getAltitude());
            STIME = Double.toString(location.getTime());
            Log.e(TAG,onLocationChanged:+ +定位本地时间);
            mLastLocation.set(位置);
            ArrayList的<&的NameValuePair GT;的NameValuePair =新的ArrayList<&的NameValuePair GT;(5);
            nameValuePair.add(新BasicNameValuePair(纵横,SLAT));
            nameValuePair.add(新BasicNameValuePair(经度,sLong));
            nameValuePair.add(新BasicNameValuePair(IMEI,IMEI));
            nameValuePair.add(新BasicNameValuePair(ReportDate,本地时间));
            nameValuePair.add(新BasicNameValuePair(海拔高度,盐));
            字符串资源;
            串LOC = URLEn coder.en code(IMEI);
            尝试{
                RES = CustomHttpClient.sendData(URL,的NameValuePair);
                字符串RESP = res.toString();
                RESP = resp.replaceAll(\\\\ S +,);
                Log.i(TAG,板条+,+ sLong +,+ RESP +,+盐);
                //Toast.makeText(getApplicationContext(),\"Coordenadas:\"+sLat+,+ sLong +设备的IMEI:+ IMEI +RESP+ RESP,Toast.LENGTH_SHORT).show();
            }赶上(例外五){
                e.printStackTrace();
            }
        }        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            Log.e(TAG,onProviderDisabled:+供应商);
        }        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            Log.e(TAG,onProviderEnabled:+供应商);
        }        @覆盖
        公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
            Log.e(TAG,onStatusChanged:+供应商);
        }
    }    LocationListener的[] mLocationListeners =新LocationListener的[] {
            新LocationListener的(LocationManager.GPS_PROVIDER)
            新LocationListener的(LocationManager.NETWORK_PROVIDER)
    };    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回null;
    }    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        共享preferences preferences = preferenceManager.getDefaultShared preferences(本);
        手机= preferences.getString(手机,);
        位置code = preferences.getString(位置code,);
        Log.e(TAGonStartCommand);
        super.onStartCommand(意向,旗帜,startId);
        返回START_STICKY;
    }    @覆盖
    公共无效的onCreate(){        Log.e(TAG的onCreate);
        initializeLocationManager();
        尝试{
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,
                    mLocationListeners [1]);
        }赶上(java.lang.SecurityException异常前){
            Log.i(TAG,不要求位置更新,忽略,前);
        }赶上(抛出:IllegalArgumentException前){
            Log.d(TAG,网络提供商不存在,+ ex.getMessage());
        }
        尝试{
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,
                    mLocationListeners [0]);
        }赶上(java.lang.SecurityException异常前){
            Log.i(TAG,不要求位置更新,忽略,前);
        }赶上(抛出:IllegalArgumentException前){
            Log.d(TAG,全球定位系统提供者不存在+ ex.getMessage());
        }
        TelephonyManager mngr =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        IMEI = mngr.getDeviceId();
    }    @覆盖
    公共无效的onDestroy(){
        Log.e(TAG的onDestroy);
        super.onDestroy();
        如果(mLocationManager!= NULL){
            的for(int i = 0; I< mLocationListeners.length;我++){
                尝试{
                    mLocationManager.removeUpdates(mLocationListeners [I]);
                }赶上(例外前){
                    Log.i(TAG,无法删除位置listners,忽略,前);
                }
            }
        }
    }    私人无效initializeLocationManager(){
        Log.e(TAGinitializeLocationManager);
        如果(mLocationManager == NULL){
            mLocationManager =(的LocationManager)getApplicationContext()getSystemService(Context.LOCATION_SERVICE)。
        }
    }
}

对不起,迟到的答复...

I want to send a GPS location to the server every 5 minutes,

Even when my application is off.

I understand AlertManager can preform this.

would it be the same efficiency wise to set a LocationMaanger with

minTime = 5 min minGPS_delta = 0

in general when would you use:

1) View.postDelay();

2) LocationMaanger.setListener() with minTime = 5 min and minGPS_delta = 0

3) AlertManager

4) Timer

解决方案

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class MyService extends Service
{
    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 300000;
    private static final float LOCATION_DISTANCE = 328.08f;
    String sLat;
    String sLong;
    String Speed;
    String sAlt;
    String sTime;
    String IMEI;
    String Phone;
    String LocationCode;
    String localTime;

    private class LocationListener implements android.location.LocationListener {
        Location mLastLocation;

        public LocationListener(String provider) {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-7:00"));
            Date currentLocalTime = cal.getTime();
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date.setTimeZone(TimeZone.getTimeZone("GMT-7:00"));
            localTime = date.format(currentLocalTime);
            sLat = Double.toString(location.getLatitude());
            sLong = Double.toString(location.getLongitude());
            Speed = Double.toString(location.getSpeed());
            sAlt = Double.toString(location.getAltitude());
            sTime = Double.toString(location.getTime());
            Log.e(TAG, "onLocationChanged: " + location + localTime);
            mLastLocation.set(location);
            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
            nameValuePair.add(new BasicNameValuePair("Latitude", sLat));
            nameValuePair.add(new BasicNameValuePair("Longitude", sLong));
            nameValuePair.add(new BasicNameValuePair("IMEI", IMEI));
            nameValuePair.add(new BasicNameValuePair("ReportDate", localTime));
            nameValuePair.add(new BasicNameValuePair("Altitude", sAlt));
            String res;
            String loc = URLEncoder.encode(IMEI);
            try {
                res = CustomHttpClient.sendData("url", nameValuePair);
                String resp = res.toString();
                resp = resp.replaceAll("\\s+", "");
                Log.i(TAG, sLat + " , " + sLong + " , " + resp +" , "+ sAlt);
                //Toast.makeText(getApplicationContext(),"Coordenadas:"+sLat+", "+sLong+" Device IMEI: "+IMEI+" resp "+resp,Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }

    LocationListener[] mLocationListeners = new LocationListener[]{
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        Phone = preferences.getString("Phone", "");
        LocationCode = preferences.getString("LocationCode", "");
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate() {

        Log.e(TAG, "onCreate");
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[1]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
        TelephonyManager mngr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        IMEI = mngr.getDeviceId();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}

Sorry for late reply...

这篇关于当应用程序关闭时将LocationListener的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆