【问题标题】:Run Gps as background service and send coordinates to web server (PHP)将 Gps 作为后台服务运行并将坐标发送到 Web 服务器 (PHP)
【发布时间】:2014-03-12 19:21:42
【问题描述】:

我知道有很多关于这个问题的问题,我已经经历了几天的所有问题,但找不到合理的答案。我是 android 和 php 的新手,所以我不知道事情是如何完成的。

所以基本上首先我想知道如何在后台运行 gps 作为服务,例如在安装应用程序时它会启动并定期将坐标发送到 Web 服务器?

【问题讨论】:

    标签: php android gps


    【解决方案1】:

    只需创建一个始终在后台运行的服务。

    例如:-

    AndroidLocationServices

    public class AndroidLocationServices extends Service {
    
    WakeLock wakeLock;
    
    private LocationManager locationManager;
    
    public AndroidLocationServices() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    
        PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);
    
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
    
        // Toast.makeText(getApplicationContext(), "Service Created",
        // Toast.LENGTH_SHORT).show();
    
        Log.e("Google", "Service Created");
    
    }
    
    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    
        Log.e("Google", "Service Started");
    
        locationManager = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
    
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                5000, 5, listener);
    
    }
    
    private LocationListener listener = new LocationListener() {
    
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
    
            Log.e("Google", "Location Changed");
    
            if (location == null)
                return;
    
            if (isConnectingToInternet(getApplicationContext())) {
                JSONArray jsonArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
    
                try {
                    Log.e("latitude", location.getLatitude() + "");
                    Log.e("longitude", location.getLongitude() + "");
    
                    jsonObject.put("latitude", location.getLatitude());
                    jsonObject.put("longitude", location.getLongitude());
    
                    jsonArray.put(jsonObject);
    
                    Log.e("request", jsonArray.toString());
    
                    new LocationWebService().execute(new String[] {
                            Constants.TRACK_URL, jsonArray.toString() });
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    };
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    
        wakeLock.release();
    
    }
    
    public static boolean isConnectingToInternet(Context _context) {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
    
        }
        return false;
    }
    
    }
    

    LocationWebService

    public class LocationWebService extends AsyncTask<String, String, Boolean> {
    
    public LocationWebService() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected Boolean doInBackground(String... arg0) {
    
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("location", arg0[1]));
    
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(arg0[0]);
        HttpParams httpParameters = new BasicHttpParams();
    
        httpclient = new DefaultHttpClient(httpParameters);
    
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
            HttpResponse response;
            response = httpclient.execute(httppost);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
    
                Log.e("Google", "Server Responded OK");
    
            } else {
    
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return null;
    }
    }
    

    您可以在这个 github 项目androidbackgroundgps中找到一个带有 Android 应用程序和相应 WebFiles 的示例项目

    【讨论】:

    • 你的 PHP 应该捕获这个请求并存储包含用户纬度和经度的 JSON
    • 在java中使用httpclient ??
    • 是的..它有效...如果你愿意,你可以尝试搜索'从 android 到 php 进行 webservice 调用'
    • 我已经实现了您所要求的确切要求。
    • 大家好,我已经在github上发布了这段代码github.com/atishtechage/backgroundgps请从那里拉出来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    相关资源
    最近更新 更多