2014-04-16 151 views
0

我正在創建一個對話框,該對話框在GPS服務被禁用的情況下應該對用戶顯示。但是發生的是,雖然我手動禁用了服務以強制對話出現,但應用程序啓動並且沒有任何事情發生。對話框不顯示

下面的代碼顯示了我如何嘗試創建對話框以及何時。請讓我知道是否有任何錯誤。

JavaCode:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gpstest00); 

    locMgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
    gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    if (!gpsEnable) { 
     showGPSDialogueBox(); 
    } 
    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this.locationListener); 
} 

/*if (savedInstanceState == null) { 
    getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
}*/ 

private void showGPSDialogueBox() { 
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this); 
    alertDialogue.setTitle("GPS Settings"); 
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + to settings menu to activate it?"); 

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 
    }); 

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      dialog.cancel(); 

     } 
    }); 
}// Enf of showDialogueBox function. 

回答

1

你需要調用顯示功能的對話框,顯示

alertDialogue.show(); 
0

你需要從AlertDialog.Builder對象創建AlertDialog然後顯示使用AlertDialogshow()方法如下。 ..

AlertDialog dialog = alertDialogue.create(); 
dialog.show(); 

更新您的showGPSDialogueBox()如下...

private void showGPSDialogueBox() { 
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this); 
    alertDialogue.setTitle("GPS Settings"); 
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + 
      "     to settings menu to activate it?"); 

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 
    }); 

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      dialog.cancel(); 

     } 
    }); 

    AlertDialog dialog = alertDialogue.create(); 
    dialog.show(); 
}