我有類GPSTracker擴展Service實現LocationListener。 Insinde onLocationChanged(),我有一個線程,我想顯示吐司「ABC」,我該怎麼做? Plz幫助我?如何在服務實現LocatonListener的線程中顯示Toast?
public void onLocationChanged(Location location) {
//Log.d(TAG, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude);
// update location here
String regChildID = SharePreferenceData.getCheckedRegister(mContext);
if (regChildID.split(",")[0].equalsIgnoreCase("1")) {
try {
String locationData;
String regID = regChildID.split(",")[2];
locationData = URLEncoder.encode("reg_child_id", "UTF-8") + "=" + URLEncoder.encode(regID.substring(0, regID.length()-1), "UTF-8");
locationData += "&" + URLEncoder.encode("lat", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(latitude), "UTF-8");
locationData += "&" + URLEncoder.encode("long", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(longitude), "UTF-8");
numberRequest = numberRequest +1;
final String data = locationData;
new Thread(new Runnable() {
@Override
public void run() {
String response = HttpRequest.sendData(Def.HTTP_METHOD_POST, Def.LOCATION_API, data);
if (response != null) {
//Log.d(TAG, "Response from server: " + data);
Log.d(TAG, "Response from server: " + response);
Log.d(TAG, "Number request update: " + numberRequest);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "asd", Toast.LENGTH_SHORT).show();
}
});
}else {
Log.d(TAG, "Number request update: " + "No response from server");
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
==================================>更新我的解決方案
謝謝大家,我剛剛做到了。我把,我在構造器類擴展服務調用上下文,內螺紋我做這樣的代碼,謝謝@busylee
new Thread(new Runnable() {
@Override
public void run() {
...
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, "abc", Toast.LENGTH_SHORT).show();
}
});
...
}).start();
不,我想是不是酷的傢伙什麼的,但你確定要從服務中顯示吐司? –