我正在开发一个小部件,它将获取当前 GPS 位置并将此值传递给远程 PHP 页面以获取信息并将其显示在小部件中。这就是我想要做的。
我在为 appWidget 实现位置监听器时遇到问题。它没有与当前位置一起更新,而是显示了初始小部件,即“正在加载小部件”(我在这里放了这个文本)
我们可以为 AppWidgetProvider 实现位置监听器吗?
或者
你能建议我在 App Widget 中获取 GPS 位置的可能解决方案吗?
我在 list 文件中放置了所有必要的权限。
这是我的代码 fragment :
public class test extends AppWidgetProvider {
static LocationManager locMan;
static Location curLocation;
static Boolean locationChanged;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service {
// GPS Listener Class
LocationListener gpsListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Log.w("GPS", "Started");
if (curLocation == null) {
curLocation = location;
locationChanged = true;
}
if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
locationChanged = false;
else
locationChanged = true;
curLocation = location;
if (locationChanged)
locMan.removeUpdates(gpsListener);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
// Log.w("GPS", "Location changed", null);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
if (status == 0)// UnAvailable
{
} else if (status == 1)// Trying to Connect
{
} else if (status == 2) {// Available
}
}
};
// In service start method, I am registering for GPS Updates
@Override
public void onStart(Intent intent, int startId) {
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
} else {
this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
}
if (curLocation != null) {
double lat = curLocation.getLatitude();
double lng = curLocation.getLongitude();
Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
// Build the widget update for today
RemoteViews updateViews = buildUpdate(this);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this,InKakinadaWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
public RemoteViews buildUpdate(Context context) {
// Here I am updating the remoteview
return updateViews;
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
提前致谢...
带着敬意,
拉加文德拉 K.
最佳答案
我看到的问题是您试图在调用 requestLocationUpdates() 之后立即检查 onStart() 中的 curLocation。 LocationManager.requestLocationUpdates() 是异步的,这意味着您的 LocationListener 在 onStart() 返回之前不会被回调。让您的 LocationListener 在收到位置后更新 UI,不要尝试在 onStart 中执行此操作。
关于android - 在android appwidget中实现位置监听器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1801866/