【发布时间】:2014-02-12 09:06:33
【问题描述】:
在我的应用程序中,如果 GPS 已关闭且应用程序启动,则会出现一个警报对话框,其中包含“GPS 设置”和“取消”按钮,如果 GPS 已打开且应用程序将启动,则不会发生任何事情。在应用程序中,有一个显示 GPS 状态的按钮,即如果 GPS 开启,它将显示“GPS 开启”,如果关闭“GPS 关闭,打开它”,当用户单击它时,它将打开 GPS 设置当用户启用 GPS 并返回时,按钮必须显示“GPS 已开启”。当用户启用 GPS 时,我发现在按钮上显示状态时遇到问题。请帮忙。我是安卓新手。
private Button GPSState;
GPSState = (Button) findViewById(R.id.bGPSstate);
boolean isGPSEnabled = false;
LocationManager locationManager;
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled) {
// no GPS provider is enabled
// displaying GPS status on the button and and opening GPS Settings
GPSState.setText("GPS is OFF.Turn it On");
GPSState.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
// creating alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("Settings");
builder.setMessage("Enable GPS for the Application");
builder.setPositiveButton("GPS Setting",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("How to use Application")
.setMessage(
"You must enable the GPS in order to use this application. Press Activate and then press Power Button twice in order to send the alert message to the selected contacts")
.setNeutralButton(
"OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
// do something // for
// returning back to //
// application
dialog.cancel();
}
}).show();
dialog.dismiss();
}
});
builder.show();
} else { // GPS provider is enabled }
GPSState.setText("GPS is On");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
GPSState.setText("GPS is on");
}
【问题讨论】: