Problem while implement location listener in android appwidget(在android appwidget中实现位置监听器时出现问题)
问题描述
我正在开发一个小部件,它将获取当前 GPS 位置并将此值传递给远程 PHP 页面以获取信息并将其显示在小部件中.这就是我想要做的.
I am working on a Widget which will get the Current GPS location and pass this value to remote PHP page to get the Information and display it in the Widget. This is what I am trying to do.
我在为 appWidget 实现位置侦听器时遇到问题.它没有与当前位置一起更新,而是显示初始小部件,即正在加载小部件"(我在这里放置此文本)
I am facing problem while implementing Location Listener for appWidget. It is not updating with the Current Location and It is showing the initial widget i.e "Loading Widget"(Here I put this text)
我们可以为 AppWidgetProvider 实现位置监听器吗?
或者
您能否建议我在 App Widget 中获取 GPS 位置的可能解决方案?
我在清单文件中放置了所有必要的权限.
这是我的代码片段:
<代码>
I placed all necessary permissions in Manifest file.
Here is my code snippet:
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) + "
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;
}
}
}
在此先感谢...
问候,Raghavendra K.
With Regards, Raghavendra K.
推荐答案
你是在模拟器还是在设备上遇到这个问题?如果在模拟器上,请确保使用 DDMS 设置 GPS 位置.但最简单的做法可能是在设备上运行您的代码,看看您是否以这种方式获得 GPS 更新.
Are you having this problem in the emulator or on a device? If on the emulator, make sure you use DDMS to set a GPS location. But the easiest thing to do might be to run your code on a device to see if you get GPS updates that way.
这篇关于在android appwidget中实现位置监听器时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android appwidget中实现位置监听器时出现问题
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01