Android

1、 获取经纬度高程

  1. /**
  2. * 启动GPS传感器一次,用于获取位置、高程
  3. */
  4. public void startLocationSensorOnce(Activity activity) {
  5. LocationManager locationManager;
  6. locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
  7. String locationProvider = null;
  8. //获取所有可用的位置提供器
  9. List<String> providers = locationManager.getProviders(true);
  10. if (providers.contains(LocationManager.GPS_PROVIDER)) {
  11. //如果是GPS
  12. locationProvider = LocationManager.GPS_PROVIDER;
  13. } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
  14. //如果是Network
  15. locationProvider = LocationManager.NETWORK_PROVIDER;
  16. } else {
  17. System.out.println("----");
  18. }
  19. if (locationProvider != null) {
  20. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  21. System.out.println("没有权限");
  22. return;
  23. }
  24. Location location = locationManager.getLastKnownLocation(locationProvider);
  25. if(location == null){
  26. Toast.makeText(this,"无法获取位置",Toast.LENGTH_LONG).show();
  27. }else {
  28. Toast.makeText(this,"lon:"+location.getLongitude()+",lat:"+location.getLatitude()+",alt:"+location.getAltitude(),Toast.LENGTH_LONG).show();
  29. }
  30. }
  31. }

2、SQLite

  • DatabaseHelper
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2. public DatabaseHelper(Context context){
  3. super(context,"test1.db",null,1);
  4. }
  5. /**
  6. * 建表
  7. */
  8. @Override
  9. public void onCreate(SQLiteDatabase db) {
  10. db.execSQL("CREATE TABLE information(\n" +
  11. " id INTEGER PRIMARY KEY NOT NULL,\n" +
  12. " name TEXT NOT NULL" +
  13. " )");
  14. db.execSQL("INSERT into information(id,name) VALUES (202001,\"张三\");");
  15. db.execSQL("INSERT into information(id,name) VALUES (202002,\"王乐\",\"男\");");
  16. db.execSQL("INSERT into information(id,name) VALUES (202003,\"刘小慧\");");
  17. }
  18. @Override
  19. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  20. }
  21. }
  • Test
  1. private void insertDb(){
  2. ContentValues values=new ContentValues();
  3. values.put("id","10004");
  4. values.put("name","testInsert");
  5. writableDatabase.insert("information",null,values);
  6. System.out.println("数据插入成功");
  7. }
  8. private void queryDb(){
  9. Cursor cursor = writableDatabase.rawQuery("select * from information where id = ?",new String[]{"10004"});
  10. // //判断结果集中是否有数据,有:查询成功;无:查询失败
  11. if(cursor.getCount()!=0){
  12. //循环遍历结果集,取出数据,显示出来
  13. while (cursor.moveToNext()){
  14. String id = cursor.getString(cursor.getColumnIndex("id"));
  15. String name = cursor.getString(cursor.getColumnIndex("name"));
  16. System.out.println(id+"--"+name);
  17. }
  18. }
  19. cursor.close();
  20. }
  21. private void updateDb(){
  22. ContentValues values=new ContentValues();
  23. values.put("name","666");
  24. writableDatabase.update("information",values,"id=?",new String[]{"10004"});
  25. System.out.println("数据更新成功");
  26. }
  27. private void deleteDb(){
  28. writableDatabase.delete("information","id=?",new String[]{"10004"});
  29. System.out.println("数据删除成功");
  30. }

保存图片

  • DatabaseHelper
  1. @Override
  2. public void onCreate(SQLiteDatabase db) {
  3. db.execSQL("CREATE TABLE information(\n" +
  4. " id INTEGER PRIMARY KEY NOT NULL,\n" +
  5. " name TEXT NOT NULL,\n" +
  6. " images BLOB"+
  7. " )");
  8. }
  • Test
  1. private ImageView showIcon;
  2. showIcon = findViewById(R.id.imageView);
  3. private void insertDb(){
  4. ContentValues values=new ContentValues();
  5. InputStream open = null;
  6. ByteArrayOutputStream os = null;
  7. try {
  8. open = this.getAssets().open("test.jpg");
  9. os = new ByteArrayOutputStream();
  10. IoUtil.copy(open,os);
  11. values.put("id","10004");
  12. values.put("name","testInsert");
  13. values.put("images",os.toByteArray());
  14. writableDatabase.insert("information",null,values);
  15. System.out.println("数据插入成功");
  16. }catch (IOException e){
  17. e.printStackTrace();
  18. }finally {
  19. try {
  20. if(os != null){
  21. os.close();
  22. }
  23. if(open != null){
  24. open.close();
  25. }
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. private void queryDb(){
  32. Cursor cursor = writableDatabase.rawQuery("select * from information where id = ?",new String[]{"10004"});
  33. // //判断结果集中是否有数据,有:查询成功;无:查询失败
  34. if(cursor.getCount()!=0){
  35. //循环遍历结果集,取出数据,显示出来
  36. while (cursor.moveToNext()){
  37. String id = cursor.getString(cursor.getColumnIndex("id"));
  38. String name = cursor.getString(cursor.getColumnIndex("name"));
  39. byte[] in = cursor.getBlob(cursor.getColumnIndex("images"));
  40. Bitmap bit = BitmapFactory.decodeByteArray(in, 0, in.length);
  41. showIcon.setImageBitmap(bit);
  42. System.out.println(id+"--"+name);
  43. }
  44. }
  45. cursor.close();
  46. }