Android的激活GPS与AlertDialog:如何等待用户采取行动? [英] Android activate gps with AlertDialog: how to wait for the user to take action?

查看:164
本文介绍了Android的激活GPS与AlertDialog:如何等待用户采取行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过不同的岗位,有没有办法等待用户采取行动的时候了 AlertDialog 显示,因为它会阻止用户界面。

I've read different posts that there is no way to wait for the user to take an action when an AlertDialog is shown because it blocks the UI.

然而,像的Facebook 例如应用程序显示 GPS是当前被禁用。是否要启用GPS?的警告对话框,并等待用户preSS是/否。 我想,它可能使用一个只包含GPS警告对话框2个不同的活动,第一个,但是这看起来不正确和definetely好好尝试一下似乎是Facebook的方式做到这一点。

However, apps like Facebook for example displays the Gps is currently disabled. Do you want to enable gps? alert dialog and wait for the user to press yes/no. I'm thinking that it's possible to use 2 different activities, first one containing only the gps alert dialog, but this doesn't seem right and definetely doens't seem the way facebook does this.

谁能告诉我怎样才能做到这一点?

Can anyone tell me how can i achieve this?

这是我的code:

       @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();
    }

所以,在我的$ C C $的 AsynTask 启动imediately无需等待用户激活GPS。 (这是一个正常的行为)

So, in my code the AsynTask starts imediately without waiting for the user to activate the gps. (which is a normal behaviour)

感谢您!

推荐答案

我设法得到这个干活得到启发这篇文章在这里,我发现了stackoverlfow通过。的http://developmentality.word$p$pss.com/2009/10/31/android-dialog-box-tutorial/

I managed to get this to work.I got inspired by this article here which i found on stackoverlfow. http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/

基本上,我感动,我需要在警告对话框中的是和取消按钮执行的逻辑。 表演所需要的是,取消逻辑按钮后,我开始asyncronous逻辑执行。

I basically moved the logic that i needed to execute on the "Yes" and "Cancel" buttons of the alert Dialog. After performing logic needed for Yes and Cancel buttons i start the asyncronous logic execution.

下面是我的code:

public interface ICommand
{
    void execute();
}

用于启用GPS和取消的警告对话框中的按钮,这两个具体的命令:

The two concrete Commands used for Enable Gps and Cancel buttons of the alert Dialog:

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();
    }
}

而现在,从活动:

And now, from the Activity:

//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();
}

现在从活动的OnCreate methodi只要致电:

Now from the Activity OnCreate methodi just call:

@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.
    }
}

我真的希望这会帮助你,当你被卡住在这一点上。)

I really hope this will help you when you get stuck at this point :).

干杯

这篇关于Android的激活GPS与AlertDialog:如何等待用户采取行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆