0
如果GPS關閉且應用程序啓動,將出現一個帶有「GPS設置」和「取消」按鈕的警報框,如果用戶選擇「GPS設置」並啓用GPS,在MainActivity上顯示「GPS is on」而不是「GPS is OFF.Turn it On」。如果GPS開啓並且應用程序啓動,它將顯示「GPS is on」。現在在應用程序中時,如果用戶決定啓用或禁用GPS,他只需點擊所顯示的GPS狀態的按鈕,它會重定向他GPS設置,當他啓用或禁用GPS並返回,按鈕必須顯示當前GPS狀態。Android啓用或禁用時在按鈕上顯示GPS狀態
現在唯一讓我困擾的部分是在GPS關閉,應用程序啓動和用戶能夠通過它的警告框,按鈕GPS的狀態不改變,休息的工作fine.Please指導我
GPSState = (Button) findViewById(R.id.bGPSstate);
boolean isGPSEnabled = false;
// Declaring a Location Manager
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 activity
GPSState.setText("GPS is off.turn it on");
GPSState.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivityForResult(
new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
REQUESTCODE);
}
});
// 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 {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
boolean isGPSEnabled = false;
GPSState = (Button) findViewById(R.id.bGPSstate);
LocationManager locationManager;
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled)
GPSState.setText("GPS is off.Turn it on");
else
GPSState.setText("GPS is on");
你應該檢查活動的onResume()中的GPS狀態,並在那裏更新你的按鈕。 –
@AdhikariBishwash非常感謝您的建議。它的工作 –
我該如何將你標記爲答案? –