关于Android GPS和SQLite的一些问题

时间:2017-06-04 08:45:17

标签: android sqlite gps

1)我有以下代码(最初点击是假的):

void configure_button(){
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                    ,10);
        }
        return;
    }
    // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            if (clicked){
                locationManager.removeUpdates(listener);
            }
            else {
                locationManager.requestLocationUpdates("gps", 5000, 0, listener);
            clicked=true;}
            distance=0;
            clicked=true;
            preloc=null;
            time1=c.get(Calendar.SECOND);
        }
    });
}

Android studio显示此错误:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException.

但是,如果locationManager.requestLocationUpdates("gps", 5000, 0, listener); 在if之外,在if之前,代码工作正常。有什么问题?

2)如何通知android是时候升级SQLite数据库了?换句话说,Android何时调用onUpgrade?

3)当找到坐标时,它们有许多小数点,但是当它们和SQLite数据库(存储为两个浮点数)一起存储和恢复时,它们只有4个。例如,41.13507153 ...将存储为41.1350。这是正常的吗?这足以获得准确的位置吗?

1 个答案:

答案 0 :(得分:-1)

1)我相信你可以重做else子句中的检查,即它说不能确定检查代码将始终进行,因为你正在设置一个独立于设置的代码运行的监听器听众。

我建议阅读Android check permission for LocationManager

  

2)如何通知android是时候升级SQLite了   数据库?换句话说,Android什么时候调用onUpgrade?

当版本号增加时调用

onUpgrade,当版本号减少时,还会调用onDowngrade

简而言之,您可以通过将传递的版本号作为第4个参数更改为SQLiteOpenHelper的super来指定何时。

示例1 将DBVERSION更改为2,以便调用onUpgrade。

public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;

public class DBHelper extends SQLiteOpenHelper {


    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    .....
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ''''
    }

示例2 通过帮助程序传递的版本

public class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context, String dbname, int version) {
            super(context, dbname, null, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        .....
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ''''
        }

活动代码;更改DBVERSION会将新版本传递给帮助程序并调用onUpgrade

public static final String DBNAME = "mydb";
public static final String DBVERSION = 1;

DBHleper myhelper = new DBHelper(this,DBNAME,DBVERSION);

要注意的一件事是许多教程的代码如下: -

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TblLogin);
        onCreate(db);
    }

哪会有效破坏任何现有数据。因此,如果您要升级数据库并且不想丢失数据,则必须包含满足此要求的代码,因为上述代码只会更改结构。

  

3)这是正常的吗?

根据SQLite文档,根据链接: -

  

REAL。该值是浮点值,存储为8字节IEEE   浮点数。

当REAL,DOUBLE,DOUBLE PRECISION或FLOAT指定为列类型时,列将为REAL(实际上以下情况适用:如果列的声明类型包含任何字符串“REAL”,“FLOA” “或”DOUB“然后该列具有REAL亲和力。  但这是第4条规则;其他规则,例如INT的规则1将使INTREAL成为INTEGER)

这可能是感兴趣的Datatypes In SQLite Version 3 ,这是上述基础。

你可能正在做的是通过cursor.getString(columnindex)从光标获取数据,我认为这会降低精度。要获得更高的精度,请使用getDouble(columnindex);

e.g。我做了(匆匆放在一起): -

MyDBHelper mydbhelper = new MyDBHelper(this);
    mydbhelper.insertRow(1.3);
    mydbhelper.insertRow(1);
    mydbhelper.insertRow(5.674389123459834);

    Cursor getfloats = mydbhelper.getAllMyFloats();
    Log.d("TESTFLOAT","Rows returned from getALlFloats = " + getfloats.getCount());
    while (getfloats.moveToNext()) {
        Log.d("TESTFLOAT","Via getString = " + getfloats.getString(getfloats.getColumnIndex(mydbhelper.MYFLOATCOL)));
        Log.d("TESTFLOAT","Via getFloat = " + Float.toHexString(
                getfloats.getFloat(
                        getfloats.getColumnIndex(
                                mydbhelper.MYFLOATCOL
                        )
                )
        ));
        Log.d("TESTFLOAT","Via getDouble = " +Double.toString(
                getfloats.getDouble(
                        getfloats.getColumnIndex(
                                mydbhelper.MYFLOATCOL
                        )

                )));
    }
    getfloats.close();

MyDBHelper: -

public class MyDBHelper extends SQLiteOpenHelper {

    public static final String DBNname = "mydb";
    public static final int DBVersion = 1;
    public static final String TESTFLOATTABLE = "testfloat";
    public static final String STDIDCOL = "_id INTEGER PRIMARYKEY";
    public static final String MYFLOATCOL = "myfloat";
    public static final String MYFLOATTYPE = " FLOAT DEFAULT 0.0";

    public MyDBHelper(Context context) {
        super(context,DBNname,null,DBVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " +
                TESTFLOATTABLE +
                "(" +
                STDIDCOL +
                "," +
                MYFLOATCOL +
                MYFLOATTYPE +
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldeversion, int newversion) {

    }

    public long insertRow(double myfloatvalue) {
        long rv;
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(MYFLOATCOL,myfloatvalue);
        rv = db.insert(TESTFLOATTABLE,null,cv);
        return rv;
    }

    public Cursor getAllMyFloats() {
        Cursor rv;
        SQLiteDatabase db = getReadableDatabase();
        rv = db.query(TESTFLOATTABLE,null,null,null,null,null,null);
        return rv;
    }
}

第一次运行的结果(后续运行将添加另一组行,依此类推): -

06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Rows returned from getALlFloats = 3
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getString = 1.3
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getFloat = 0x1.4cccccp0
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getDouble = 1.3
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getString = 1
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getFloat = 0x1.0p0
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getDouble = 1.0
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getString = 5.67439
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getFloat = 0x1.6b293p2
06-05 10:49:35.279 21951-21951/mjt.testvcsuse D/TESTFLOAT: Via getDouble = 5.674389123459834

因此,查看最后3行getString,得到5dp,而getDouble达到15dp(不确定最大精度,快速查看15-17)。