我已经阅读了不同的帖子,在显示AlertDialog
时无法等待用户采取操作,因为它会阻止用户界面。
但是,例如Facebook
等应用程序会显示当前已禁用 Gps。是否要启用gps?警告对话框并等待用户按是/否。
我认为可以使用2个不同的活动,第一个只包含gps警告对话框,但这似乎不正确,并且看起来不像facebook这样做。
谁能告诉我怎样才能实现这一目标?
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitializeComponents();
EnableGPSIfPossible();
ListAsync lAsync = new ListAsync(this);
lAsync .execute();
}
private void EnableGPSIfPossible()
{
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
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();
}
因此,在我的代码中,AsynTask
即时启动而无需等待用户激活gps。 (这是正常行为)
谢谢!
答案 0 :(得分:12)
你实际可以做的是:
GPSManager.java:
public class GPSManager {
private Activity activity;
private LocationManager mlocManager;
private LocationListener gpsListener;
public GPSManager(Activity activity) {
this.activity = activity;
}
public void start() {
mlocManager = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
setUp();
findLoc();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
activity);
alertDialogBuilder
.setMessage("GPS is disabled in your device. Enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
public void setUp() {
gpsListener = new GPSListener(activity, mlocManager);
}
public void findLoc() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
gpsListener);
if (mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) == null)
Toast.makeText(activity, "LAST Location null", Toast.LENGTH_SHORT)
.show();
else {
gpsListener.onLocationChanged(mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER));
}
}
}
GPSListener.java:
public class GPSListener implements LocationListener {
private Activity activity;
private LocationManager lm;
private int numberOfUpdates;
public static final int MAX_NUMBER_OF_UPDATES = 10;
public GPSListener(Activity activity, LocationManager lm) {
this.activity = activity;
this.lm = lm;
}
@Override
public void onLocationChanged(Location loc) {
if (numberOfUpdates < MAX_NUMBER_OF_UPDATES) {
numberOfUpdates++;
Log.w("LAT", String.valueOf(loc.getLatitude()));
Log.w("LONG", String.valueOf(loc.getLongitude()));
Log.w("ACCURACY", String.valueOf(loc.getAccuracy() + " m"));
Log.w("PROVIDER", String.valueOf(loc.getProvider()));
Log.w("SPEED", String.valueOf(loc.getSpeed() + " m/s"));
Log.w("ALTITUDE", String.valueOf(loc.getAltitude()));
Log.w("BEARING", String.valueOf(loc.getBearing() + " degrees east of true north"));
String message;
if (loc != null) {
message = "Current location is: Latitude = "
+ loc.getLatitude() + ", Longitude = "
+ loc.getLongitude();
// lm.removeUpdates(this);
} else
message = "Location null";
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
} else {
lm.removeUpdates(this);
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity, "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity, "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
然后从你的活动开始:
GPSManager gps = new GPSManager(
yourActivity.this);
gps.start();
答案 1 :(得分:7)
我是通过使用简单的功能完成的,但没有显示警告框我将usercontrol重定向到设置页面
以下是我使用的功能
public void isGPSEnable(){
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
答案 2 :(得分:5)
我设法让这个工作。我从这篇文章中受到了启发,我在stackoverlfow上找到了它。 http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/
我基本上移动了我需要在警报对话框的“是”和“取消”按钮上执行的逻辑。 在执行Yes和Cancel按钮所需的逻辑后,我启动异步逻辑执行。
这是我的代码:
public interface ICommand
{
void execute();
}
用于启用警报对话框的Gps和取消按钮的两个具体命令:
public class CancelCommand implements ICommand
{
protected Activity m_activity;
public CancelCommand(Activity activity)
{
m_activity = activity;
}
public void execute()
{
dialog.dismiss();
//start asyncronous operation here
}
}
public class EnableGpsCommand extends CancelCommand
{
public EnableGpsCommand( Activity activity) {
super(activity);
}
public void execute()
{
// take the user to the phone gps settings and then start the asyncronous logic.
m_activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
super.execute();
}
}
现在,来自活动:
//returns true if the GpsProviderIsDisabled
//false otherwise
private boolean EnableGPSIfPossible()
{
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
return true;
}
return false;
}
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 CommandWrapper(new EnableGpsCommand(this)))
.setNegativeButton("No", new CommandWrapper(new CancelCommand(this)));
final AlertDialog alert = builder.create();
alert.show();
}
现在来自Activity OnCreate methodi,请致电:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newfriendlist);
InitializeComponents();
if (!EnableGPSIfPossible())
{
//then gps is already enabled and we need to do StartFriendRetrievalAsync from here.
//otherwise this code is being executed from the EnableGpsIfPossible() logic.
//Asyncronous logic here.
}
}
我真的希望这会在你遇到困难时帮助你:)。
干杯
答案 3 :(得分:3)
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "your message";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
getActivity().startActivity(new Intent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
答案 4 :(得分:1)
(对不起我的英文) 当我看到你的问题并且你的问题给出答案时,我正在寻找解决方案。
问题是当对话框警报打开时,活动继续启动,当用户按下&#34; YES&#34;新的活动将开始&#34; gps的设置..&#34;当用户在设置ON他的gps之后回到第一个活动或者只是忽略了你,这个相同的活动将不会做任何更新或认为某些事情(比如gps on)已经发生,因为它已经加载了,所以你必须听取更新并应用更改,或者只是在用户&#34;返回&#34; ,对我来说,我刚刚补充道:
<activity
...
android:noHistory="true"
... /> </activity>
现在,每次用户按下&#34;后退按钮&#34;考虑到新的更新,您的活动将重新开始 它对我有用,希望这可以帮助你或帮助其他任何人