
我正在开发一个Widget,它将获取当前GPS位置并将此值传递给远程PHP页面以获取信息并将其显示在Widget中.这就是我想要做的.
我在为appWidget实现Location Listener时遇到了问题.它不是用当前位置更新它显示初始小部件,即“正在加载小部件”(这里我把这个文本)
我们可以为appwidgetprovider实现位置监听器吗?
要么
你能否告诉我在App Widget中获取GPS位置的可能解决方案?
我在Manifest文件中放置了所有必要的权限.
这是我的代码片段:
public class test extends appwidgetprovider {static LocationManager locMan;static Location curLocation;static Boolean locationChanged;@OverrIDepublic 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; }}}提前致谢…
带着敬意,
Raghavendra K.
解决方法:
您是在模拟器中还是在设备上遇到此问题?如果在模拟器上,请确保使用DDMS设置GPS位置.但最简单的方法可能是在设备上运行您的代码,看看您是否以这种方式获得GPS更新.
总结以上是内存溢出为你收集整理的在android appwidget中实现位置监听器时出现问题全部内容,希望文章能够帮你解决在android appwidget中实现位置监听器时出现问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)