2015-09-09 41 views
0

我设计这帖子,如果(启用GPS和网络启用)的GPS坐标的Android应用程序。目前我没有编写发布数据的代码。 我已经编写了代码,用于后台服务和调用服务。通过一些教程。任何一个可以请法官这是正确的方式连续GPS android系统中,每x分钟跟踪即使应用程序被关闭(或强制关闭)

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

尝试创建延伸应用类......何况这类清单文件......然后检查 – koutuk

+0

所以AndroidLocationServices应该扩展应用? – user1936201

+0

没有创建新类扩展应用程序而不是服务/活动 – koutuk

回答

2

有没有办法让服务运行在如果用户强制关闭或关闭应用程序,则为背景。

为了提高不被杀害的服务的机会时,在内存不足的系统运行,我会建议使它foreground。您还需要将其设置为前景以避免在Android棉花糖中暂停(请参阅Power-Saving Optimizations)。

您还可以看看Traccar Client open source GPS tracking app。你可以将它作为你的应用的基础,或者只是从那里借鉴一些想法。

+1

上述代码是否工作(有问题的代码),如果在后台运行?或者我应该做任何改变。调用我的登录屏幕startService(新的Intent(this,AndroidLocationServices.class));每次 ?请帮助 – user1936201

+1

我认为它应该工作。从你调用'startService'的地方并不重要。 Android系统将确保只有一个服务实例。 –

+0

请帮助接受答案,代码是否可以在以下条件下使用? 1.连续跟踪GPS Coodrinates如果应用程序在前台和后台运行。 2.如果应用程序被强制杀死(它不会像你所说的那样工作),但是当应用程序打开并开始时,它应该重新启动(连续跟踪GPS Coodrinates)。 3.我应该做些什么改变。 请帮助。 – user1936201