我想显示对话框,它会暂停活动。等待用户打开GPS。之后,它可以查询数据库以填充列表。
下面描述了我想做的事情
这是我的代码
public class RestaurantListFragment extends ListFragment {
private ArrayList<Restaurant> restaurants;
private SQLDataHelper dataHelper;
GPSTracker gpsTracker;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.restaurant_list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gpsTracker = new GPSTracker(getActivity());
if(!gpsTracker.canGetLocation)
{
showSettingsGPSAlert();
}
dataHelper = new SQLDataHelper(getActivity(), "restaurantDB");
restaurants = new ArrayList<Restaurant>();
dataHelper.openDB();
Cursor cursor = dataHelper.query("Restaurant", new String[]{"Id", "ResName", "Logo", "Address", "Latitude", "Longitude"}, null
, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Restaurant restaurant = new Restaurant();
restaurant.setId(cursor.getInt(0));
restaurant.setResName(cursor.getString(1));
restaurant.setLogo(cursor.getString(2));
restaurant.setAddress(cursor.getString(3));
restaurants.add(restaurant);
} while (cursor.moveToNext());
}
//Collections.sort(restaurants);
RestaurantAdapter restaurantAdapter = new RestaurantAdapter(getActivity(), restaurants);
setListAdapter(restaurantAdapter);
}
public void showSettingsGPSAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
我的问题是它不等待用户打开GPS。它显示带有对话并发的列表。我找了很多解决方案。但它不起作用。请帮忙!
答案 0 :(得分:0)
问题是因为您正在检查GPS但是下面只有您放置代码来更新列表。您可以尝试此方法。使用位置监听器已覆盖方法onProviderDisabled
和onProviderEnabled
。如果您检查GPS设置,打开“设置”页面后,将调用onProviderEnabled
。因此,请使用该方法更新列表视图
修改强>
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locationListener = new MyLocationListener();
Call requestLocationUpdates:
之后lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0, // ms since last call
0, // m movement
locationListener);
现在定义LcationListener:
class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider) {
Log.d("Hello7","onProviderEnabled");
//Update your listview here
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Hello10","onProviderEnabled");
}
}
您也可以lm.removeUpdates(locationListener);