简体   繁体   English

警报以启用GPS和网络,位置

[英]Alert to enable gps and network , location

I want to test if the gps is enabled and if so to show an alert 我想测试是否启用了gps,是否启用了警报

            protected void createGpsDisabledAlert() {
    AlertDialog builder = new AlertDialog.Builder(this).create();
    builder.setMessage("Your GPS is disabled! Would you like to enable it?");

    builder.setButton("Enable GPS", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            showGpsOptions();
        }

    });
    builder.setButton2("Do nothing", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            return;
        }
    });

    builder.show();
}

private void showGpsOptions() {
    Intent gpsOptionsIntent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(gpsOptionsIntent);

}

I did debug and it seems everything ok , i have no error , but the alert does not appear, even if it eneters in method 我进行了调试,似乎一切正常,没有错误,但是即使方法中出现警报,也不会出现警报

Thank you in advance 先感谢您

I have created a very resuseful approach! 我创建了一个非常有用的方法! Just configure onResume for each activity according its needed resources. 只需根据每个活动的所需资源配置onResume。 This approach going to alert the user to enable resources and provide shortcuts to the resource settings. 这种方法将提醒用户启用资源并提供资源设置的快捷方式。 The user can do nothing still everything is enabled. 即使启用了所有功能,用户也无法执行任何操作。

UPDATE: http://code.google.com/p/android-resource-checker/ 更新: http : //code.google.com/p/android-resource-checker/

check this code i add the GPS is enabled or disabled in Toast....... 检查此代码,我添加Toast中启用或禁用GPS的功能。

import android.app.Activity;
import android.content.*;
import android.location.*;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.widget.*;

public class DirectionsWhereisparty extends Activity {
    double destin_lat = 0, destin_lng = 0;
    double current_lat = 0, current_lng = 0;
    boolean flag = true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                mlocListener);
    }

    public void go(double c_lat, double c_lng, double d_lat, double d_lng) {

        Bundle b = getIntent().getExtras();

        if (b != null) {

            String lat = b.getString("lat");
            String lng = b.getString("lng");

            d_lat = Double.parseDouble(lat);
            d_lng = Double.parseDouble(lng);

            // System.out.println("d_lat " + d_lat);
            // System.out.println("d_lng " + d_lng);

            String Text = "My current location is: " + "Latitude = " + c_lat
                    + "Longitude = " + c_lng;

            Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                    .show();

            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri
                    .parse("http://maps.google.com/maps?saddr=" + c_lat
                            + "   ,  " + c_lng + "&daddr=" + d_lat + "   ,   "
                            + d_lng));
            startActivity(intent);
        }
    }

    public class MyLocationListener implements LocationListener

    {

        public void onLocationChanged(Location loc) {

            current_lat = loc.getLatitude();
            current_lng = loc.getLongitude();

            if (flag == true) {
                flag = false;
                go(current_lat, current_lng, destin_lat, destin_lng);
            }

        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();

        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM