1

Фоновая служба для определения местоположения закрывается через некоторое время. У меня есть код в onTaskRemoved, чтобы установить будильник для отложенного запроса. Я все еще не могу решить проблему. Ниже приведен код моего фона класса обслуживания:Фоновая служба Android для местоположения не работает в других телефонах

public class MyService extends Service { 
    public static final String BROADCAST_ACTION = "Hello World"; 
    private static final int TWO_MINUTES = 1000 * 60 *5; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 
    double latitude; // latitude 
    double longitude; // longitude 
    Intent intent; 
    private static final String POST_URL = "http://api.url.com/oauth/api/v1/location/update"; 
    int counter = 0; 
    public static boolean isRunning = false; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 


     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     listener = new MyLocationListener(); 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     super.onCreate(); 

     return (IBinder) intent; 
    } 




    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      return true; 
     } 

     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
     boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
     boolean isNewer = timeDelta > 0; 

     if (isSignificantlyNewer) { 
      return true; 
     } else if (isSignificantlyOlder) { 
      return false; 
     } 

     // Check whether the new location fix is more or less accurate 
     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
     boolean isLessAccurate = accuracyDelta > 0; 
     boolean isMoreAccurate = accuracyDelta < 0; 
     boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

     // Check if the old and new location are from the same provider 
     boolean isFromSameProvider = isSameProvider(location.getProvider(), 
       currentBestLocation.getProvider()); 

     // Determine location quality using a combination of timeliness and accuracy 
     if (isMoreAccurate) { 
      return true; 
     } else if (isNewer && !isLessAccurate) { 
      return true; 
     } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
      return true; 
     } 
     return false; 
    } 


    /** 
    * Checks whether two providers are the same 
    */ 
    private boolean isSameProvider(String provider1, String provider2) { 
     if (provider1 == null) { 
      return provider2 == null; 
     } 
     return provider1.equals(provider2); 
    } 


    @Override 
    public void onDestroy() { 

     super.onDestroy(); 

Toast.makeText(getApplicationContext(),"onDestroy",Toast.LENGTH_LONG).show(); 
Log.d("onDestroy","onDestroy"); 

     Intent myIntent = new Intent(getApplicationContext(), MyService.class); 

     PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

     AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

     Calendar calendar = Calendar.getInstance(); 

     calendar.setTimeInMillis(System.currentTimeMillis()); 

     calendar.add(Calendar.SECOND, 1); 

     alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10, pendingIntent); 



    } 


    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     super.onTaskRemoved(rootIntent); 
     Toast.makeText(getApplicationContext(),"onTaskRemoved",Toast.LENGTH_LONG).show(); 
     Log.d("onTaskRemoved", "onTaskRemoved"); 
     Intent myIntent = new Intent(getApplicationContext(), MyService.class); 

     PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

     AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

     Calendar calendar = Calendar.getInstance(); 

     calendar.setTimeInMillis(System.currentTimeMillis()); 

     calendar.add(Calendar.SECOND, 1); 

     alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10, pendingIntent) ; 




    } 


    public class MyLocationListener implements LocationListener { 

     public void onLocationChanged(final Location loc) { 
      if (isBetterLocation(loc, previousBestLocation)) { 
       latitude = loc.getLatitude(); 
       longitude = loc.getLongitude(); 
       intent.putExtra("Latitude", loc.getLatitude()); 
       intent.putExtra("Longitude", loc.getLongitude()); 
       intent.putExtra("Provider", loc.getProvider()); 
       sendBroadcast(intent); 


       SharedPreferences check = getApplicationContext().getSharedPreferences("Access_token", Context.MODE_PRIVATE); 
       final String access_token = check.getString("access_token", "N/A"); 

       StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL, 
         new Response.Listener<String>() { 
          @Override 
          public void onResponse(String response) { 

Toast.makeText(getApplicationContext(),""+response,Toast.LENGTH_LONG).show(); 
          } 
         }, 
         new Response.ErrorListener() { 
          @Override 
          public void onErrorResponse(VolleyError error) { 

           Toast.makeText(getApplicationContext(),""+error,Toast.LENGTH_LONG).show(); 

          } 
         }) { 
        @Override 
        protected Map<String, String> getParams() { 
         Map<String, String> params = new HashMap<String, String>(); 
         params.put("access_token", access_token); 
         params.put("latitude", String.valueOf(latitude)); 
         params.put("longitude", String.valueOf(longitude)); 
         return params; 
        } 

       }; 
       stringRequest.setRetryPolicy(new DefaultRetryPolicy(1 * 500000, 
         DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
         DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
       RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
       requestQueue.add(stringRequest); 
      } 


     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    } 

} 
+0

Я вижу несколько проблем с вашим кодом. Вы не можете вызвать 'super.onCreate()' из 'onBind()'. Не используйте 'onStart()', поскольку это было устарело 100 лет назад. Внесите 'onStartCommand()' и верните 'START_STICKY'. –

+0

Знаете ли вы, какие устройства имеют эту проблему? –

ответ

0

возвращения в нуле onbind метода как возвращающее что-то означает, что вы хотите, чтобы привязать его к компоненту, и это может привести к остановке работы на закрытии этого компонента. Также используйте onStartCommand вместо onStart. return START_REDELIVER_INTENT, если вам нужно намерение в методе OnStartCommand, используйте START_STICKY, чтобы просто перезапустить службу.

public class MyService extends Service { 
    public static final String BROADCAST_ACTION = "Hello World"; 
    private static final int TWO_MINUTES = 1000 * 60 *5; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 
    double latitude; // latitude 
    double longitude; // longitude 
    Intent intent; 
    private static final String POST_URL = "http://api.url.com/oauth/api/v1/location/update"; 
    int counter = 0; 
    public static boolean isRunning = false; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    this.intent = new Intent(BROADCAST_ACTION); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    listener = new MyLocationListener(); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
    } 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); 
    return START_STICKY; 
    } 

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     return true; 
    } 

    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    if (isSignificantlyNewer) { 
     return true; 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
     currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return true; 
    } 
    return false; 
    } 


    /** 
    * Checks whether two providers are the same 
    */ 
    private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
    } 


    @Override 
    public void onDestroy() { 

    super.onDestroy(); 

    Toast.makeText(getApplicationContext(),"onDestroy", Toast.LENGTH_LONG).show(); 
    Log.d("onDestroy","onDestroy"); 

    Intent myIntent = new Intent(getApplicationContext(), MyService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.add(Calendar.SECOND, 1); 

    alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10, pendingIntent); 



    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
    return null; 
    } 


    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 
    Toast.makeText(getApplicationContext(),"onTaskRemoved",Toast.LENGTH_LONG).show(); 
    Log.d("onTaskRemoved", "onTaskRemoved"); 
    Intent myIntent = new Intent(getApplicationContext(), MyService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.add(Calendar.SECOND, 1); 

    alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10, pendingIntent) ; 

    } 


    public class MyLocationListener implements LocationListener { 

    public void onLocationChanged(final Location loc) { 
     if (isBetterLocation(loc, previousBestLocation)) { 
     latitude = loc.getLatitude(); 
     longitude = loc.getLongitude(); 
     intent.putExtra("Latitude", loc.getLatitude()); 
     intent.putExtra("Longitude", loc.getLongitude()); 
     intent.putExtra("Provider", loc.getProvider()); 
     sendBroadcast(intent); 


     SharedPreferences check = getApplicationContext().getSharedPreferences("Access_token", Context.MODE_PRIVATE); 
     final String access_token = check.getString("access_token", "N/A"); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

       Toast.makeText(getApplicationContext(),""+response,Toast.LENGTH_LONG).show(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       Toast.makeText(getApplicationContext(),""+error,Toast.LENGTH_LONG).show(); 

       } 
      }) { 
      @Override 
      protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("access_token", access_token); 
      params.put("latitude", String.valueOf(latitude)); 
      params.put("longitude", String.valueOf(longitude)); 
      return params; 
      } 

     }; 
     stringRequest.setRetryPolicy(new DefaultRetryPolicy(1 * 500000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
     requestQueue.add(stringRequest); 
     } 


    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
    } 

}