在我的应用程序中,如果GPS已关闭且应用程序已启动,则会出现一个警告对话框,其中包含“GPS设置”和“取消”按钮,如果GPS已打开且应用程序将启动,则不会发生任何事情。在应用程序中,有一个显示GPS状态的按钮,即如果GPS打开,它将显示“GPS已开启”,如果关闭“GPS已关闭,请将其打开”,当用户点击它时,它将打开GPS设置当用户启用GPS并返回时,按钮必须显示“GPS已开启”。当用户启用GPS时,我发现在按钮上显示状态时遇到问题。请帮忙。我是android的新手。
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");
}
答案 0 :(得分:2)
使用startActivityForResult
代替startActivity
。用户在“设置”页面中启用/禁用GPS后,他/她将按后退按钮返回到您的应用。在这一刻,方法onActivityResult
被调用。您可以在此处设置文字。
如何设置文字?粗略地说:
onActivityResult() {
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");
else GPSState.setText("GPS is on");
}
目前我没有看到android文档或eclipse。但我认为这可以给你一个线索。我知道方法不是那样的。
答案 1 :(得分:0)
使用此代码:
public static boolean isGpS(Context mContext)
{
final LocationManager manager = (LocationManager)mContext.getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
{
return false;
}
else
{
return true;
}
}
在你的onCreate中写下这个:
if(!Utils.isGpS(Youractivity.this)
{
buildAlertMessageNoGps();
}
这是showAlertDialog的方法:
private void buildAlertMessageNoGps()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
注意:不要忘记在AndroidMenifest文件中应用权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>