Android
1、 获取经纬度高程
/**
* 启动GPS传感器一次,用于获取位置、高程
*/
public void startLocationSensorOnce(Activity activity) {
LocationManager locationManager;
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = null;
//获取所有可用的位置提供器
List<String> providers = locationManager.getProviders(true);
if (providers.contains(LocationManager.GPS_PROVIDER)) {
//如果是GPS
locationProvider = LocationManager.GPS_PROVIDER;
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
//如果是Network
locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
System.out.println("----");
}
if (locationProvider != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
System.out.println("没有权限");
return;
}
Location location = locationManager.getLastKnownLocation(locationProvider);
if(location == null){
Toast.makeText(this,"无法获取位置",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(this,"lon:"+location.getLongitude()+",lat:"+location.getLatitude()+",alt:"+location.getAltitude(),Toast.LENGTH_LONG).show();
}
}
}
2、SQLite
- DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context){
super(context,"test1.db",null,1);
}
/**
* 建表
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(\n" +
" id INTEGER PRIMARY KEY NOT NULL,\n" +
" name TEXT NOT NULL" +
" )");
db.execSQL("INSERT into information(id,name) VALUES (202001,\"张三\");");
db.execSQL("INSERT into information(id,name) VALUES (202002,\"王乐\",\"男\");");
db.execSQL("INSERT into information(id,name) VALUES (202003,\"刘小慧\");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
- Test
private void insertDb(){
ContentValues values=new ContentValues();
values.put("id","10004");
values.put("name","testInsert");
writableDatabase.insert("information",null,values);
System.out.println("数据插入成功");
}
private void queryDb(){
Cursor cursor = writableDatabase.rawQuery("select * from information where id = ?",new String[]{"10004"});
// //判断结果集中是否有数据,有:查询成功;无:查询失败
if(cursor.getCount()!=0){
//循环遍历结果集,取出数据,显示出来
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println(id+"--"+name);
}
}
cursor.close();
}
private void updateDb(){
ContentValues values=new ContentValues();
values.put("name","666");
writableDatabase.update("information",values,"id=?",new String[]{"10004"});
System.out.println("数据更新成功");
}
private void deleteDb(){
writableDatabase.delete("information","id=?",new String[]{"10004"});
System.out.println("数据删除成功");
}
保存图片
- DatabaseHelper
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(\n" +
" id INTEGER PRIMARY KEY NOT NULL,\n" +
" name TEXT NOT NULL,\n" +
" images BLOB"+
" )");
}
- Test
private ImageView showIcon;
showIcon = findViewById(R.id.imageView);
private void insertDb(){
ContentValues values=new ContentValues();
InputStream open = null;
ByteArrayOutputStream os = null;
try {
open = this.getAssets().open("test.jpg");
os = new ByteArrayOutputStream();
IoUtil.copy(open,os);
values.put("id","10004");
values.put("name","testInsert");
values.put("images",os.toByteArray());
writableDatabase.insert("information",null,values);
System.out.println("数据插入成功");
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(os != null){
os.close();
}
if(open != null){
open.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void queryDb(){
Cursor cursor = writableDatabase.rawQuery("select * from information where id = ?",new String[]{"10004"});
// //判断结果集中是否有数据,有:查询成功;无:查询失败
if(cursor.getCount()!=0){
//循环遍历结果集,取出数据,显示出来
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
byte[] in = cursor.getBlob(cursor.getColumnIndex("images"));
Bitmap bit = BitmapFactory.decodeByteArray(in, 0, in.length);
showIcon.setImageBitmap(bit);
System.out.println(id+"--"+name);
}
}
cursor.close();
}