Android读书笔记(四) LitePal、运行时权限、通知的几点说明

1.LitePal的基本用法

  编辑app/bulid.gradle文件,在dependencies中添加内容如下:

dependencies {
    compile 'org.litepal.android:core:1.6.0'
}

  配置litepal.xml,在Project目录方式下,项目目录→app→src→main,右键main→New→Diretory,Diretory名为assets,在assets目录下创建litepal.xml文件。在litepal.xml中进行设置,设置如下:

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="StudentManagement"></dbname>
    <version value="1"></version>

    <list>
        <mapping class="cn.xd.study.a3.Student"></mapping>
    </list>
</litepal>

  dbname 数据库名称,version 数据库版本,list映射模型。list所设置的类是数据库中的表,Student类就对应数据库中的Student表。

  最后设置一下AndroidManifest.xml。

 <application
        android:name="org.litepal.LitePalApplication"
        android:label="@string/app_name"
        ......................>
 </application>

Student类

public class Student {

    private int id;
    private String name;
    private String gender;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

  任意的数据库操作都会使得在没有数据库的情况下,自动把数据库创建出来。
  如果Student类的属性增加了,我们只需要将数据库版本加一就行了,而且原有的数据还存在。

CURD操作
  要进行CURD操作,模型类(Student类)必须继承DataSupport类。

public class Student  extends DataSupport{
   .........................
}
  • 添加数据
                Student student = new Student();
                student.setId(110);
                student.setName("张三");
                student.setGender("男");
                student.save();
  • 更新数据

  此种更新只能对已存储的对象进行操作。

                Student student = new Student();
                student.setId(110);
                student.setName("张三");
                student.setGender("男");
                student.save();
                student.setGender("女");
                student.save();

  将id为110的Student的Gender置为女。

                Student student = new Student();
                student.setGender("女");
                student.updateAll("id = ?","110");

  将数据更新成默认值,要使用setToDefault()方法。

 student.setToDefault("gender");
  • 删除数据

  删除student表中id为110的数据。

     DataSupport.deleteAll(Student.class,"id = ?","110");
  • 查询数据

  查询Student表的所有数据。

 List<Student> students = DataSupport.findAll(Student.class);

  获取表中第一条数据

 Student students = DataSupport.findFirst(Student.class);

  获取表中最后一条数据

Student students = DataSupport.findLast(Student.class);

  查询Student表的name这一列数据

 List<Student> students = DataSupport.select("name").find(Student.class);

  查询Student表中gender为女的数据

 List<Student> students = DataSupport.where("gender=?","女").find(Student.class);

  除了上面所写的,还有排序、指定查询数量等。

 List<Student> students = DataSupport.order("name desc").find(Student.class);

 List<Student> students = DataSupport.limit(3).find(Student.class);

  LitePal支持原生SQL

Cursor cursor = DataSupport.findBySQL("select * from Student where  gender=?","");

2.运行时权限

  在6.0及以上系统使用危险权限时都必须使用运行时权限处理。运行时权限是在程序运行过程中由用户授权的操作。

                //判断用户是否授权,
                if (ActivityCompat.checkSelfPermission(Main4Activity.this
                        , Manifest.permission.CALL_PHONE) !=
                         PackageManager.PERMISSION_GRANTED) {
                    //参数一:Activity实例,参数二:在数组中的权限名称,参数三:一般置1
                    ActivityCompat.requestPermissions(Main4Activity.this
                            , new String[]{Manifest.permission.CALL_PHONE}, 1);
                } else {
                    call();//打电话
                }

  如果用户没有授权,调用ActivityCompat.requestPermissions(),系统会弹出对话框来询问是否授权,选择后,程序会跳到onRequestPermissionsResult()方法中,grantResults中存放了授权的结果。接下来判断授权结果,做出相应的动作。

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] ==
                 PackageManager.PERMISSION_GRANTED) {//授权
                    call();
                } else {
                    Toast.makeText(Main4Activity.this, "你禁止了权限",
                     Toast.LENGTH_SHORT).show();
                }

                break;
            default:
        }

3.通知的几点说明

  为了更好的兼容性,请使用support库中的NotificationCompat.Builder。

  NotificationCompat.Builder notification = new 
                                NotificationCompat.Builder(Main5Activity.this);
  ..............................
  nManager.notify(1, notification.build());

  使用BigTextStyle、BigPictureStyle、InboxStyle 等大布局的Style,也要使用 NotificationCompat中的。
  关于一些使用NotificationCompat中Style的通知,不能显示的原因是手机系统被更改,被厂商所更改,如:红米1s(该换盆了)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值