即使应用程序关闭(或强制关闭),在Android中每x分钟连续gps跟踪

时间:2015-09-09 11:26:48

标签: android gps android-service

我正在设计一个Android应用程序,它会发布GPS坐标(启用GPS并启用网络)。目前我没有编写发布数据的代码。 我编写了代码,用于后台服务和调用服务。通过一些教程。可以任何一个请法官这是正确的方式

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.media.AudioManager;
    import android.media.Ringtone;
    import android.media.RingtoneManager;
    import android.media.ToneGenerator;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.PowerManager;
    import android.os.PowerManager.WakeLock;
    import android.os.Vibrator;
    import android.util.Log;
    import android.widget.Toast;

    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
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("Google", "Service Started");
             Toast.makeText(getApplicationContext(), "Service Started",
                     Toast.LENGTH_SHORT).show();

            locationManager = (LocationManager) getApplicationContext()
                    .getSystemService(Context.LOCATION_SERVICE);

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    15*1000,0, listener);
            return Service.START_STICKY;
        }


    //  @Override
    //  @Deprecated
    //  public void onStart(Intent intent, int startId) {
    //      // TODO Auto-generated method stub
    //      super.onStart(intent, startId);
    //
    //      Log.e("Google", "Service Started");
    //       Toast.makeText(getApplicationContext(), "Service Started",
    //               Toast.LENGTH_SHORT).show();
    //
    //      locationManager = (LocationManager) getApplicationContext()
    //              .getSystemService(Context.LOCATION_SERVICE);
    //
    //      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    //              15*1000,0, 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){
                     Toast.makeText(getApplicationContext



    (), "Location null",
                             Toast.LENGTH_SHORT).show();
                    return;

                }else{
                     Toast.makeText(getApplicationContext(), "Location null else",
                             Toast.LENGTH_SHORT).show();


                }
                try {
                    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                    r.play();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (Util.isGPSAvi() && Util.isNetAci()) {

                    // JSONArray jsonArray = new JSONArray();
                    // JSONObject jsonObject = new JSONObject();

                    try {
                        Log.e("latitude", location.getLatitude() + "");
                        Log.e("longitude", location.getLongitude() + "");

                        Toast.makeText(
                                getApplicationContext(),
                                "Latitude : " + location.getLatitude()
                                        + " :: Longitude : "
                                        + location.getLongitude(),
                                Toast.LENGTH_LONG).show();

                        // 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();
                         Toast.makeText(getApplicationContext(), "In Exp "+e.getMessage(),
                                 Toast.LENGTH_SHORT).show();
                    }

                }

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "onProviderDisabled",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "onProviderEnabled",Toast.LENGTH_LONG).show();

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "onStatusChanged",Toast.LENGTH_LONG).show();

            }
        };

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Toast.makeText(getApplicationContext(), "onDestroy",Toast.LENGTH_LONG).show();
            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;
    //  }



    }

在启动屏幕应用程序登录按钮onClick

@Override
public void onClick(View v) {

    startService(new Intent(this, AndroidLocationServices.class));

    And starting intent to go for another screen 


}  

和清单文件

 <service
        android:name="com.xxx.gps.AndroidLocationServices"
        android:enabled="true" />

0 个答案:

没有答案