在我的應用程序中,如果GPS關閉並且應用程序啓動,將出現一個警報對話框,其中包含按鈕「GPS設置」和「取消」,並且如果GPS打開並且應用程序將不會啓動發生。在應用程序中,有一個顯示GPS狀態的按鈕,即如果GPS打開,它將顯示「GPS已打開」,如果關閉「GPS關閉,打開它」,當用戶單擊它時,它將打開GPS設置當用戶啓用GPS並返回時,按鈕必須顯示「GPS已打開」。當用戶啓用GPS時,我在按鈕上顯示狀態時遇到麻煩。請幫忙。我是android新手。Android在按鈕上顯示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");
}
我完成了startActivityForResult,請告訴我如何設置文本onActivityResult。 –
我已經更新了我的回答 – ardhipoetra
非常感謝。它有幫助。實際上我在onActivityResult中感到困惑。非常感謝 –