【问题标题】:Android dialog pause activityAndroid 对话框暂停活动
【发布时间】:2013-05-18 04:25:57
【问题描述】:

我想显示对话框,它会暂停活动。等待用户打开 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 。它显示带有对话框并发的列表。我搜索了很多解决方案。但它不起作用。请帮忙!

【问题讨论】:

  • 你的意思是要等到用户打开gps?如果你说得更准确一些,我也许可以帮助你。
  • 是的,我非常想要
  • 很抱歉回复太晚了,但是您应该在对话框中设置 setCancelable(false)。添加一个监听 GPS 是否打开的 BroadcastReceiver。 (应该有一个)并且每当您的 BroadcastReceiver 收到 GPS 已打开时,取消您的对话并继续。或者阻止任何其他操作,直到您从 LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) 收到 true;

标签: android concurrency dialog sequence wait


【解决方案1】:

问题是因为您正在检查 GPS,但下面只有您放置代码来更新列表。您可以尝试这种方法。使用具有覆盖方法 onProviderDisabledonProviderEnabled 的位置侦听器。打开设置页面后,如果您检查 GPS 设置,则会调用onProviderEnabled。所以用那个方法更新你的列表视图

编辑:

LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
       MyLocationListener locationListener = new MyLocationListener();

调用后请求LocationUpdates:

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); 停止 locationUpdates

【讨论】:

  • 是的,你能解释更多细节吗?或者在您的答案中添加一些代码。谢谢你
  • 但是,如果像你说的那样检查。它不会等到用户打开 GPS 然后查询数据库来填写 listview 。它可以跳过下面的代码。我可以通过调用 if(!gpsTracker.canGetGPS()) { showSettingsGPSAlert(); 来检查 GPS }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多