我剛開始使用Google地圖,我試圖在GPS關閉時顯示一個對話框。 這是我的onProviderDisabled方法的代碼。onProviderDisabled對話框未顯示
@Override
public void onProviderDisabled(String provider) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS is disabled");
builder.setC
ancelable(false);
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent startGPS = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(startGPS);
}
});
builder.setNegativeButton("Leave GPS off", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
我也已將此添加到OnCreate中...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapSettings();
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
if(provider == null)
{
onProviderDisabled(provider);
}
}
感謝您尋找。 :-)