我已经阅读了不同的帖子,AlertDialog因为它阻止了UI ,所以无法等待用户在显示时执行操作.
但是,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();
}
Run Code Online (Sandbox Code Playgroud)
因此,在我的代码中,AsynTask无需等待用户激活gps即可立即启动.(这是正常行为)
谢谢!
Ale*_* C. 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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
然后从你的活动:
GPSManager gps = new GPSManager(
yourActivity.this);
gps.start();
Run Code Online (Sandbox Code Playgroud)
我通过使用简单的功能,但没有显示警报框,我将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);
}
}
Run Code Online (Sandbox Code Playgroud)
我设法让这个工作.我从这篇文章中得到了启发,我在stackoverlfow上找到了它.http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/
我基本上移动了我需要在警报对话框的"是"和"取消"按钮上执行的逻辑.在执行Yes和Cancel按钮所需的逻辑后,我启动异步逻辑执行.
这是我的代码:
public interface ICommand
{
void execute();
}
Run Code Online (Sandbox Code Playgroud)
用于启用警报对话框的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();
}
}
Run Code Online (Sandbox Code Playgroud)
而现在,来自活动:
//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();
}
Run Code Online (Sandbox Code Playgroud)
现在来自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.
}
}
Run Code Online (Sandbox Code Playgroud)
我真的希望这会在你遇到困难时帮助你:).
干杯
| 归档时间: |
|
| 查看次数: |
26396 次 |
| 最近记录: |