gpt4 book ai didi

Android 使用 AlertDialog : how to wait for the user to take action? 激活 gps

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:46 24 4
gpt4 key购买 nike

我读过不同的帖子,当显示 AlertDialog 时无法等待用户采取行动,因为它会阻塞 UI。

但是,Facebook 等应用程序显示 Gps 当前已禁用。你想启用 gps 吗? 警告对话框并等待用户按是/否。我认为可以使用 2 种不同的 Activity ,第一种仅包含 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。 (这是正常行为)

谢谢!

最佳答案

你实际上可以做的是:

全局定位系统管理器.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));
}
}
}

全局定位系统监听器.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) {

}

}

然后从您的 Activity 中:

GPSManager gps = new GPSManager(
yourActivity.this);
gps.start();

关于Android 使用 AlertDialog : how to wait for the user to take action? 激活 gps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com