Ứng dụng của tôi cần sửa chữa vị trí thường xuyên, ngay cả khi điện thoại không tỉnh táo. Để thực hiện việc này, tôi đang sử dụng IntentService với mẫu được cung cấp rộng rãi bởi Commonsware. https://github.com/commonsguy/cwac-wakefulGửi tin nhắn đến một Trình xử lý trên một chuỗi đã chết khi nhận được một vị trí từ một số IntentService
Để sửa vị trí, tôi dựa vào mã sau được cung cấp bởi thành viên có tên là Fedor. What is the simplest and most robust way to get the user's current location on Android?. Tôi hơi sửa đổi nó để trả về null thay vì nhận được vị trí cuối cùng được biết
Cả hai đều hoạt động hoàn hảo khi không kết hợp: Tôi có thể sửa vị trí từ lớp của Fedor trong Hoạt động và tôi có thể thực hiện một số nội dung cơ bản (như đăng nhập một cái gì đó) bằng cách sử dụng lớp Commonsware.
Sự cố xảy ra khi tôi đang cố gắng sửa lỗi vị trí từ lớp con WakefulIntentService của Commonsware. LocationListeners không phản ứng với locationUpdates và tôi nhận được những cảnh báo này (tôi chưa nhận được sự tinh tế của các chủ đề, trình xử lý, ...). Ai đó có thể giúp tôi hiểu vấn đề là gì không? Cảm ơn và tôi muốn tất cả các bạn một năm mới rất hạnh phúc :)
12-31 14:09:33.664: W/MessageQueue(3264): Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessage(Handler.java:383)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:193)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Binder.execTransact(Binder.java:338)
12-31 14:09:33.664: W/MessageQueue(3264): at dalvik.system.NativeStart.run(Native Method)
Đây là lớp con của tôi WakefulIntentService:
public class AppService extends WakefulIntentService {
public static final String TAG = "AppService";
public AppService() {
super("AppService");
}
public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(final Location location) {
if (location == null)
Log.d(TAG, "location could not be retrieved");
else
sendMessage(location);
}
};
@Override
protected void doWakefulWork(Intent intent) {
PhoneLocation myLocation;
myLocation = new PhoneLocation();
myLocation.getLocation(getApplicationContext(), locationResult);
}
Và đây là một bản sao của lớp Fedor của (chút thay đổi: không cần GPS hay cuối cùng biết vị trí)
public class PhoneLocation {
public static String TAG = "MyLocation";
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(network_enabled) lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new ReturnNullLocation(), 20000);
return true;
}
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class ReturnNullLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerNetwork);
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
Nó hoạt động như một nét duyên dáng! Cảm ơn bạn rất nhiều! Tôi đã thiết lập báo thức trong lớp con Ứng dụng của mình và tạo các phương thức startLocationPoll() và stopLocationPoll() tại đó. Chúc mừng năm mới! – znat
khi tôi sử dụng ví dụ trên liên kết .... tôi đã nhận được ... "E/LocationPoller (442): Mục đích không hợp lệ - không có nhà cung cấp" ... bất kỳ đầu mối nào? – user836026
@ user836026: Bạn không thể cung cấp thêm 'LocationPoller.EXTRA_PROVIDER' trên' Intent', cho biết nhà cung cấp nào sẽ sử dụng. Nếu bạn có thêm câu hỏi về thành phần này, vui lòng sử dụng nút "Đặt câu hỏi" thay vì đăng nhận xét tại đây. – CommonsWare