我需要在方法onproviderdisabled中使用以下代码。但不会持续太久。
如何等待用户响应?

AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
        builder.setMessage("Your GPS is disabled! Would you like to enable it?").setCancelable(false).setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(gpsOptionsIntent);
            }
        });
        builder.setNegativeButton("Do nothing", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

提前谢谢!!

最佳答案

像这样试试。

public void onProviderDisabled(String provider) {
     Message msg = handler.obtainMessage();
     msg.arg1 = 1;
     handler.sendMessage(msg);
}

private final Handler handler = new Handler() {
     public void handleMessage(Message msg) {
          if(msg.arg1 == 1){
               if (!isFinishing()) { // Without this in certain cases application will show ANR
                    AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
                    builder.setMessage("Your GPS is disabled! Would you like to enable it?").setCancelable(false).setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int id) {
                              Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                              startActivity(gpsOptionsIntent);
                          }
                     });
                     builder.setNegativeButton("Do nothing", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                               dialog.cancel();
                          }
                     });
                     AlertDialog alert = builder.create();
                     alert.show();
               }
           }

       }
};

关于android - 在android LocationListener中的onProviderDisabled中显示警告对话框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7090305/

10-09 07:16