1

我有应用程序从 gps 获取位置
但是如果 GPS 禁用了我的应用程序
在模拟器中强制关闭它很好不是错误,但如果在设备中运行它是强制关闭
我该怎么做?这是我的代码:

public class Track extends Activity implements LocationListener{
String curTime;
double lat;
double lng;
double alt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    final String provider = locationManager.getBestProvider(criteria, true);
    Dbhelper helper = new Dbhelper(this);
    final SQLiteDatabase db = helper.getWritableDatabase();
    updateWithNewLocation(null);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run(){
            db.isOpen();
            db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
                    "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
            //db.close();
        }
    }, 10*60*1000, 10*60*1000);
    locationManager.requestLocationUpdates(provider, (10*60*1000), 10,
                                           locationListener);
    PackageManager manager = this.getPackageManager();
    PackageInfo info = null;
    try {
        info = manager.getPackageInfo(this.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Toast.makeText(this,
      "PackageName = " + info.packageName + "\nVersionCode = "
        + info.versionCode + "\nVersionName = "
        + info.versionName + "\nPermissions = "+info.permissions, Toast.LENGTH_SHORT).show();
    System.out.println("PackageName = " + info.packageName + "\nVersionCode = "
        + info.versionCode + "\nVersionName = "
        + info.versionName + "\nPermissions = "+info.permissions);
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  public void updateWithNewLocation(Location location) {


        if (location != null) {
            Dbhelper helper = new Dbhelper(this);
            final SQLiteDatabase db = helper.getWritableDatabase();
            long time = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
            curTime = df.format(time);
            lat = location.getLatitude();
            lng = location.getLongitude();
            alt = location.getAltitude();
            System.out.println(lat);
            System.out.println(lng);
            System.out.println(alt);
            /*db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
            "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
            db.close();*/
            /*Timer timer = new Timer();
            timer.schedule(new TimerTask(){
                @Override
                public void run(){
                    db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
                            "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
                    db.close();
                }
            }, 10*60*1000, 10*60*1000);*/

          } 
       }
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

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

}
}

请给我一个解决方案..谢谢你的反馈:)

4

3 回答 3

3

如果你想在你的应用程序中自动开启 GPS,恐怕除了提示用户之外别无他法。

您可以通过修改 LocationListener 中的 onProviderDisabled() 方法轻松实现此目的。这个想法是打开一个对话框,要求用户打开 GPS:

public void onProviderDisabled(String arg0) 
    {
        showDialog(CHOICE_GPS_ENABLE);          
    }

添加您的活动:

    protected final static int CHOICE_GPS_ENABLE = 1; //or any other number

@Override
protected Dialog onCreateDialog(int id)
{
    Dialog dialog = null;
    switch (id)
    {
        case CHOICE_GPS_ENABLE:
            dialog = createGPSEnableDialog();
            break;

        default:
            dialog = super.onCreateDialog(id);
            break;
    }

    return dialog;
}

    protected Dialog createGPSEnableDialog()
{
    Dialog toReturnGPS;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("You don't have GPS enabled. Go to Settings and enable GPS?");

    builder.setTitle("GPS failed");

    builder.setPositiveButton("Yes, enable GPS",
            new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int which) 
                        {
                          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivity(intent);
                        }
                });

    builder.setNegativeButton("No, quit application",
            new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int which) 
                        {
                            onDestroy();
                        }
                });

    toReturnGPS = builder.create();
    return toReturnGPS;
}

希望能帮助到你。

于 2012-05-11T09:40:35.580 回答
0

如果我的应用程序正在运行,我希望 GPS 自动打开,我该怎么做?

除了提示用户打开它之外,我认为您无法做任何事情。因此,恐怕您将无法使您的应用程序按照您的意愿运行。

于 2012-05-11T03:03:01.953 回答
0

利用

Location locationtest;
     public void onLocationChanged(Location location) {
          locationtest=location;
          updateWithNewLocation(locationtest);
        }

        public void onProviderDisabled(String provider){
              locationtest= null;
              updateWithNewLocation(locationtest);
            }

代替

 public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }
于 2012-05-11T04:51:22.627 回答