2013-05-18 156 views
0

我想顯示對話框,它會暫停活動。等待用戶打開GPS。之後,它可以查詢數據庫來填充列表。Android對話框暫停活動

下面描述的圖片是我想要做的

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

這是我的代碼

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

你的意思是你想等到用戶開啓GPS嗎?如果你更精確,我可能會幫助你。 – Milan

+0

是的,它很興奮我想 –

+0

很抱歉,很晚纔回復,但是,您應該在對話框中設置setCancelable(false)。添加一個偵聽GPS是否打開的BroadcastReceiver。 (應該有一個)每當您的BroadcastReceiver接收到GPS打開時,取消您的對話框並繼續。或者阻止任何其他操作,直到您從LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)接收到true; – Milan

回答

0

問題在於您正在檢查GPS,但下面只有您放置了更新列表的代碼。你可以試試這個方法。使用位置監聽器,它已經改寫了方法onProviderDisabledonProviderEnabled。打開設置頁面後,如果您檢查GPS設置,則會調用onProviderEnabled。因此,在這種方法更新您的ListView

編輯:

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

呼叫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"); 
       } 
    } 

你也可以停止locationUpdates通過lm.removeUpdates(locationListener);

+0

是的,你能解釋更多細節嗎?或者給你的答案添加一些代碼。謝謝 –

+0

看到我編輯的答案 – Abhi

+0

但是,如果檢查像你說的。它不會等待用戶打開GPS,然後查詢數據庫以填寫listview。它可以跳過下面的代碼。我可以通過調用if來檢查GPS(!gpsTracker.canGetGPS()) { showSettingsGPSAlert(); } –