Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中,即使应用程序关闭(或强制关闭),每x分钟也可以进行连续gps跟踪_Android_Gps_Android Service - Fatal编程技术网

在android中,即使应用程序关闭(或强制关闭),每x分钟也可以进行连续gps跟踪

在android中,即使应用程序关闭(或强制关闭),每x分钟也可以进行连续gps跟踪,android,gps,android-service,Android,Gps,Android Service,我正在设计一个Android应用程序,如果(GPS启用和网络启用)发布GPS坐标。目前我还没有为发布数据编写代码。 我已经为后台服务和呼叫服务编写了代码。通过一些教程。任何人都能判断这是正确的方法吗 import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; impor

我正在设计一个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;
    //  }



    }
和清单文件

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

如果用户强制关闭或终止应用程序,则无法在后台运行服务

为了提高服务在系统内存不足时不被终止的可能性,我建议使用它。您还需要将其置于前台,以避免挂在Android棉花糖中(请参阅)


你也可以看看。您可以将其作为应用程序的基础,也可以从中借鉴一些想法。

尝试创建扩展应用程序的类。。。在清单文件中提到这个类。。。然后检查AndroidLocationServices是否应该扩展应用程序?没有创建新的类来扩展应用程序,而不是服务/活动。我应该写的是什么?如果在后台运行,上面的代码(有问题的代码)会工作吗?或者我应该做些改变。调用我的登录屏幕startService(newintent(这个,AndroidLocationServices.class))好吗;每一次?请帮帮我,我想应该有用。无论从何处调用
startService
。Android系统将确保该服务只有一个实例。请帮助接受回答,该代码在以下条件下工作吗?1.如果应用程序在前台和后台运行,则持续跟踪GPS坐标。2.若应用程序被强制关闭(它不会像你们提到的那个样工作),但当应用程序打开并启动时,它应该重新启动(持续跟踪GPS坐标)。3.我应该做些什么改变吗。请帮忙。是的,就我所知,它应该能起作用,但你当然需要试试。2.您需要重新启动服务。
 <service
        android:name="com.xxx.gps.AndroidLocationServices"
        android:enabled="true" />