1

위치 감지를위한 백그라운드 서비스가 언젠가 닫힙니다. 나는 onTaskRemoved에 pendingIntent revoking에 대한 경보를 설정하는 코드가 있습니다. 나는 아직도 문제를 해결할 수 없다. 뭔가를 반환하는 것은 당신이 구성 요소에 바인딩 할 뜻이 해당 구성 요소의 확대에 대한 서비스의 중단으로 이어질 수 있기 때문에다른 휴대 전화에서 위치에 대한 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

코드에 몇 가지 문제가 있습니다. onBind()에서 super.onCreate()를 호출 할 수 없습니다. 'onStart()'는 100 년 전에 사용되지 않았으므로 사용하지 마십시오. 'onStartCommand()'를 구현하고'START_STICKY'를 반환하십시오. –

+0

이 장치에 어떤 문제가 있는지 알고 계십니까? –

답변

0

는 null onbind 방법 : 아래 내 배경 서비스 클래스의 코드입니다. 또한 onStart 대신 onStartCommand를 사용하십시오. OnStartCommand 메소드에 인 텐트가 필요한 경우 START_REDELIVER_INTENT 플래그를 반환하고 다른 경우 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) { 

    } 
    } 

} 
관련 문제