后台服务失去了gps位置听众摊位

时间:2015-01-13 23:46:01

标签: android service gps location

我有一个后台服务,以保持丢失的GPS信号和测量距离(在计时器上,因为有很多缓慢的移动!来回和转向这个应用程序,但没有测量每个位置的变化为主要应用吸引足够的资源)  如果主应用程序因ph调用等而被杀,此服务将不会死亡。用户必须重新进入app并退出以杀死它,是的,除非stoptimer = true,否则它不会死亡

希望清理其他人有用

请注意。 prefs来控制 stoptimer = true停止服务 startmeasure = true来测量距离

写(context,allinediss,alllines);使用公共静态字符串读取(上下文上下文,最终字符串键)

在主应用程序中读取此pref获取距离

这是我的第一个应用程序的较小版本的添加,所以可能会被知道他们正在做什么以及我的调试需要改进的人改进。

ps getdistance是针对rhumb距离并且没有工作所以修复它或清理它并且我不知道添加多个小的大圆距离的错误因素(如果有的话)

String adis = Read(context,allinediss);

是空的并且抛出数字格式异常,所以添加了这个以修复它。      如果(adis.length()< 1){ADIS =" 0&#34 ;;              }

     public class Timer extends Service implements      LocationListener {
public Timer() {
}
    LocationManager locationManager ;


    double lon;
    double lat;

   Handler handler = new Handler();
 double starmeasure=2;
 double  measurecounter=0;
double alllinedis;
 double lat2;
 double lon2;
 double lat1;
 double lon1;
 double latfs;
 double lonfs;
  double time=1;
  double time1=1;
  int timercount=0;
String allinediss="allines";

DecimalFormat precision00 = new                   DecimalFormat("0.00");



@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    Toast.makeText(this, "H service started", Toast.LENGTH_LONG).show();




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

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);

    Context context = getApplicationContext();
    String timerb="timerb";
    boolean stoptimer=ReadBoolean(context,timerb,false);

if(stoptimer==false){

    Handler handler = new Handler();
    handler.postDelayed(runnable, 1000);




}   



}

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
      Toast.makeText(this, "service started", Toast.LENGTH_LONG).show();


}

@Override
public void onDestroy() {
    Toast.makeText(this, "meter service destroyed", Toast.LENGTH_LONG).show();

}



   /*
//First we need a Handler that starts the Runnable after 100ms

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);

*/

//And we also need the Runnable for the Handler

private Runnable runnable = new Runnable() {
   @Override
   public void run() {
      /* do what you need to do */


       gpstime();

      // savpointforroute();

       Context context = getApplicationContext();
       String timerb="timerb";
       boolean       stoptimer=ReadBoolean(context,timerb,false);
/* and here comes the "trick" */
       if(stoptimer==false){
      handler.postDelayed(this, 1000*20);
 }



 }   };



public void startmeasure() {

    //This will check if null and reset measure to get 2 fixes before adding distance on
    String lats=Double.toString(lat);
    if (lats.length() < 4){    

    Toast.makeText(this,"lats null", Toast.LENGTH_SHORT).show();

    starmeasure=2;

    return;

}

       //       Toast.makeText(this,  "timerlat"+lats, Toast.LENGTH_SHORT).show();



//pref to turn on  or off measure NOT reset
    Context context = getApplicationContext();
     String startmeasure="startmeasure";
    boolean startmeasureb=ReadBoolean( context, startmeasure, true);

if(startmeasureb==true){
     starmeasure=starmeasure+1;
//4 fixes before measure started    
if(starmeasure>4){

    if (measurecounter>1){
          lat1=lat;
          lon1=lon;
     //    double linedist=getRhumbLineDistance(lat2,lon2,lat,lon);

         //old lat to measure too
         Location apoint2=new Location("");

         apoint2.setLatitude(lat2);
         apoint2.setLongitude(lon2);




         //set latlng location to measure to
         Location tpoint2=new Location("");

         tpoint2.setLatitude(lat);
         tpoint2.setLongitude(lon);

    //   double linedist=getDistance( lat,  lon, lat2,  lon2) ;



            double linedist=apoint2.distanceTo(tpoint2);

        //failsafe exceed distance back to zero 
        if(linedist>200){
            linedist=0;
        }   


         String adis=Read(context,allinediss);
                     if(adis.length()<1){adis="0";
         }


         alllinedis=Double.parseDouble(adis); 


             double a=alllinedis+linedist;

         alllinedis=a;

         alllinedis=Math.round(alllinedis);

         measurecounter=0;




     }else{
        measurecounter=2; 

             lat2=lat;
         lon2=lon;
       // double linedist=getRhumbLineDistance(lat1,lon1,lat,lon);


         Location nt=new Location("");

          nt.setLatitude(lat1);
          nt.setLongitude(lon1);




         //set latlng location target to get dis to line

             Location oint=new Location("");

         oint.setLatitude(lat);
         oint.setLongitude(lon);


    //   double linedist=getDistance( lat,  lon, lat1,  lon1) ;

            double linedist=nt.distanceTo(oint);






         if(linedist>200){
         linedist=0;
         }


         // add measured to original distance
       String adis=Read(context,allinediss);
             if(adis.length()<1){adis="0";
         }


                 alllinedis=Double.parseDouble(adis); 

            double b=alllinedis+linedist;

             alllinedis=b;   

        //   alllinedis=alllinedis+linedist;


                 alllinedis=Math.round(alllinedis);

    //   Toast.makeText(this,"dis"+Double.toString( alllinedis), Toast.LENGTH_SHORT).show();



    }



         //Distance covered to pref
         String alllines=Double.toString(alllinedis);
         String allinediss="allines";
        Write( context, allinediss, alllines);
     }  
    }
    }

    private void gpstime(){



if(  time1==time ){

    Toast.makeText(this, "GPS Signal Lost", Toast.LENGTH_LONG).show();
}else{
    startmeasure();
}
time1=time;
        /*  String time1s=Double.toString(time);
Toast.makeText(this, time1s, Toast.LENGTH_LONG).show();
     */

}








// String
    public static String Read(Context context, final String key) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    return pref.getString(key, "");
}



       public static void Write(Context context, final          String key, final String value) {
       SharedPreferences settings =      PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.commit();        
   }



   // Boolean  
               public static boolean ReadBoolean(Context context, final String key, final boolean defaultValue) {
         SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
return settings.getBoolean(key, defaultValue);
 }



        public static void WriteBoolean(Context context, final String key, final boolean value) {
SharedPreferences settings =      PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
editor.commit();        
  }





     public double getDistance(double lat1, double lon1, double lat2, double lon2) 
    { 
double latA = Math.toRadians(lat1); 
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2); 
double lonB = Math.toRadians(lon2); 
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) + (Math.sin(latA) * Math.sin(latB)); 
double ang = Math.acos(cosAng); 
double dist = ang *6371; 
return dist;

 }

 //@Override
 public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

    lat =(location.getLatitude());

             lon =(location.getLongitude());
time=(location.getTime());

  //     Toast.makeText(this, "loc changed", Toast.LENGTH_LONG).show();



   //    startmeasure();



}

  @Override
 public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

  }


@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:0)

String adis = Read(context,allinediss);

是空的并且抛出数字格式异常,所以添加了这个以修复它。

如果(adis.length()&LT; 1){ADIS = “0”; }

已编辑的问题,因此可以正常使用