-1

내 프로젝트에서 백그라운드로 계속 위치를 검색하는 서비스를 사용했습니다.백그라운드에서 계속 현재 위치 얻기

public class LocationService extends Service 
{ 

    private static final int TWO_MINUTES = 1000 * 60 * 2; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 

    Intent i; 


    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
    i = new Intent("LOCATION_CHANGED"); 

    } 

    @Override 
    public int onStartCommand(Intent intent,int flags, int startId) 
    { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     listener = new MyLocationListener(); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); 
     return super.onStartCommand(intent, flags, startId); 

    } 

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

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      // A new location is always better than no location 
      return true; 

     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
     boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
     boolean isNewer = timeDelta > 0; 

     // If it's been more than two minutes since the current location, use the new location 
     // because the user has likely moved 
     if (isSignificantlyNewer) { 
      return true; 
      // If the new location is more than two minutes older, it must be worse 
     } 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() { 
     // handler.removeCallbacks(sendUpdatesToUI); 
     super.onDestroy(); 
     Log.v("STOP_SERVICE", "DONE"); 
     locationManager.removeUpdates(listener); 
    } 

    public static Thread performOnBackgroundThread(final Runnable runnable) { 
     final Thread t = new Thread() { 
      @Override 
      public void run() { 
       try { 
        runnable.run(); 
       } catch(Exception e) { 
System.out.print(e); 
       } 
      } 
     }; 
     t.start(); 
     return t; 
    } 




    public class MyLocationListener implements LocationListener 
    { 

     public void onLocationChanged(final Location loc) 
     { 
      Log.i("**************************************", "Location changed"); 
      if(isBetterLocation(loc, previousBestLocation)) { 
       String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude(); 
       i = new Intent("LOCATION_CHANGED"); 
       i.putExtra("location", newLocation); 
       sendBroadcast(i); 



      } 
     } 

     public void onProviderDisabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 


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

     } 

    } 
} 

을 다음과 같이 내가 MainActivity의 의도를 방송 수신 한 :

public class MainActivity extends Activity { 


    TextView txt_location; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txt_location = (TextView)findViewById(R.id.current_location); 


     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 


     } 

    } 



    public class LocationBroadcast extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context ctx, Intent intent) { 

      txt_location.setText(intent.getExtras().getString("location")); 
     } 

    } 



     public static class PlaceholderFragment extends Fragment { 


     public PlaceholderFragment() { 

     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 


      return inflater.inflate(R.layout.fragment_main, container, 
        false); 

     } 
    } 

} 

내가하고 싶은 모든 txt_location에 위치의 위도와 경도를 설정하는 것입니다 다음과 같이 내 LocationService이다. 이 코드가 작동하고 그대로 txt_location 표시되지 않습니다 : 당신이 그것을 어떤 위치 업데이트를 표시되지 볼 수 있듯이

enter image description here

... 로그 캣은 어떤 errors..Can의 사람이 표시되지 안내하시기 바랍니다 나 무슨 실수 야? 서비스를 시작하는

+0

사용 AndoridHive 자습서 : http://www.androidhive.info/2012/07/android -Gps-location-manager-tutorial/ –

+0

@TusharPandey 일부 버튼 클릭으로 위치를 검색합니다. 나는 (서비스 또는 다른 방법을 사용하여) 백그라운드에서 계속적으로 위치를 얻고 싶다고 말했다. – xyz

+0

서비스를 만들거나 무제한 시간 동안 백그라운드 스레드를 사용하고 위치가 변경되면 localbroadcast msg를 생성한다. –

답변

0

은 5 초에서 말한다 : 중지 경보를 들어

AlarmManager alarm =(AlarmManager)context.getSystemService(context.ALARM_SERVICE); 
     PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MonitorService.class), 0); 
     alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5*1000, pi); 

:

AlarmManager mgr =(AlarmManager)getSystemService(ALARM_SERVICE); 
     PendingIntent pi = PendingIntent.getService(context, 0, new Intent(this,MonitorService.class), 0); 
     mgr.cancel(pi); 
     pi.cancel(); 
1

사용 서비스로이 클래스. 그러나이 매니페스트

<service android:name="com.example.androidservice.beckend.EndlessService" /> 

에서 서비스 클래스를 정의하고 활동 수업 시간에 사용하기 전에

startService(new Intent (this , EndlessService.class)) ;

import android.app.Service; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Handler; 
import android.os.HandlerThread; 
import android.os.IBinder; 
import android.os.Looper; 
import android.os.Message; 
import android.os.Process; 
import android.util.Log; 
import android.widget.Toast; 

public class EndlessService extends Service { 
    private Looper mServiceLooper; 
    private ServiceHandler mServiceHandler; 

    // Handler that receives messages from the thread 
    private final class ServiceHandler extends Handler { 
     public ServiceHandler(Looper looper) { 
      super(looper); 
     } 

     @Override 
     public void handleMessage(Message msg) 
     { 
      new HitToTheInternet().execute(""); 
     } 
    } 

    @Override 
    public void onCreate() { 
     // Start up the thread running the service. Note that we create a 
     // separate thread because the service normally runs in the process's 
     // main thread, which we don't want to block. We also make it 
     // background priority so CPU-intensive work will not disrupt our UI. 
     HandlerThread thread = new HandlerThread("ServiceStartArguments",Process.THREAD_PRIORITY_BACKGROUND); 
     thread.start(); 

     // Get the HandlerThread's Looper and use it for our Handler 
     mServiceLooper = thread.getLooper(); 
     mServiceHandler = new ServiceHandler(mServiceLooper); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 

     // For each start request, send a message to start a job and deliver the 
     // start ID so we know which request we're stopping when we finish the 
     // job 
     Message msg = mServiceHandler.obtainMessage(); 
     msg.arg1 = startId; 
     mServiceHandler.sendMessage(msg); 

     if (intent == null) 
     { 
      Log.e("intentStatus", "intent is null"); 
     } 

     // If we get killed, after returning from here, restart 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // We don't provide binding, so return null 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
    } 

    class HitToTheInternet extends AsyncTask<String, String, String> 
    { 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      Log.e("doInBackground", "Running") ; 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

      new Handler().postDelayed(new Runnable() { 
       public void run() { 
        Message msg = mServiceHandler.obtainMessage(); 
        mServiceHandler.sendMessage(msg); 
       } 
      }, 10000); 
     } 
    } 
} 
+0

이 좋은 @xyz입니다. –

관련 문제