gpt4 book ai didi

android - 在 android LocationListener 的 onProviderDisabled 中显示警告对话框

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:40 27 4
gpt4 key购买 nike

我需要在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com