10

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

因此,在我的代码中,AsynTask无需等待用户激活 gps 即可立即启动。(这是正常行为)

谢谢!

4

5 回答 5

13

您实际上可以做的是:

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();
于 2012-08-20T20:25:38.783 回答
7

我通过使用简单的功能做到了这一点,但没有显示警告框,我将用户控件重定向到设置页面

这是我使用的功能

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

}
于 2012-09-14T11:24:23.360 回答
5

我设法让它发挥作用。我从 stackoverlfow 上找到的这篇文章中得到启发。http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/

我基本上移动了我需要在警报对话框的“是”和“取消”按钮上执行的逻辑。在执行“是”和“取消”按钮所需的逻辑后,我开始异步逻辑执行。

这是我的代码:

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 方法中调用:

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

我真的希望这会在您陷入困境时对您有所帮助:)。

干杯

于 2012-09-16T11:07:24.113 回答
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();
于 2015-06-12T13:01:29.097 回答
1

(对不起我的英语)当我看到你的问题并且你的问题给出了答案时,我正在寻找解决方案。

问题是在对话框警报打开时活动继续启动,当用户按下“是”时,新活动将启动“gps 设置..”当用户在设置他的 gps 后返回第一个活动或只是忽略了你,这个相同的活动不会做任何更新或认为某些事情(如 gps 开启)已经发生,因为它已经加载,所以你必须收听更新并应用更改,或者在用户“返回”时重新启动活动,对我来说,我刚刚添加:

<activity
    ...
    android:noHistory="true"
    ... /> </activity>

现在,每次用户按下“后退按钮”时,您的活动都会重新开始,考虑到它对我有用的新更新,希望这可以帮助您或帮助其他人

于 2017-04-20T15:58:14.663 回答