2013-01-09 32 views
0

我想讓我的GPS在後臺運行,所以它可以爲我提供關於我在哪裏的新信息,同時我還在做其他事情。Android:在後臺運行應用程序的問題

但每次我運行應用程序崩潰。該錯誤要我使用Looper.prepare(),但我怎麼用我的GPS來做到這一點?

public class LocationLoggerService extends Service implements LocationListener { 

Location location; 

String towers; 
static final int uniqueID = 1394885; 
Thread runner; 
boolean keepRunning; 

public LocationManager locationManager; 

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

@Override 
public void onCreate() { 

    keepRunning = true; 

    runner = new Thread(null, new Runnable() { 
     public void run() { 
      startGps(); 
     } 
    }); 
    runner.start(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // this service will run until we stop it 

    return START_STICKY; 
} 

@Override 
public void onLocationChanged(Location location) { 

    location = locationManager.getLastKnownLocation(towers); 

    String hey1 = Double.toString(location.getLatitude()); 
    String hey2 = Double.toString(location.getLongitude()); 

    Toast.makeText(this, 
      "TEST onLocationChanged:" + "Lat: " + hey1 + "Long: " + hey2, 

      Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onProviderEnabled(String s) { 
} 

@Override 
public void onProviderDisabled(String s) { 
} 

@Override 
public void onStatusChanged(String s, int i, Bundle b) { 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG) 
      .show(); 

} 

public void startGps() { 
    locationManager = (LocationManager) this 
      .getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
      2000, 0, this); 

    location = locationManager.getLastKnownLocation(towers); 

    String hey1 = Double.toString(location.getLatitude()); 
    String hey2 = Double.toString(location.getLongitude()); 

    Toast.makeText(this, "StartGPS: Lat: " + hey1 + "Long: " + hey2, 
      Toast.LENGTH_LONG).show(); 

} 


} 

我的表現有以下權限,如下圖所示:

我得到的logcat的這個錯誤,如下圖所示:

FATAL EXCEPTION: Thread-819 
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
at android.os.Handler.<init>(Handler.java:121) 
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:209) 
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:209) 
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:687) 
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512) 
at com.example.rememberforme.LocationLoggerService.startGps(LocationLoggerService.java:81) 
at com.example.rememberforme.LocationLoggerService$1.run(LocationLoggerService.java:38) 
at java.lang.Thread.run(Thread.java:856) 
+2

LogCat中的錯誤是什麼? –

+0

logcat與您的應用程序無關 - 請附上適當的logcat,以顯示您的應用程序正在運行! FWIW,這甚至不是一個錯誤 - 這是來自原生二進制NDK編譯的GPS處理程序... – t0mm13b

+0

OP:請重新編輯您的問題,而不是在這些評論中放入1個班輪logcats ...一定要包括儘可能多的儘可能讓我們***可以推斷出錯誤的位置! – t0mm13b

回答

0

Heey,

我請參閱您缺少服務的onCommandStart方法。

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    //this service will run until we stop it 

    return START_STICKY; 
} 

您是否在清單文件中添加了所需的所有權限? 我不確定你需要哪一個,但有一個或多個需要這樣做:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_GPS"/> 
+0

我剛剛更新了我的代碼。但是我遇到了一個新問題。 Looper.prepare() – Zaz