码迷,mamicode.com
首页 > 移动开发 > 详细

安卓小案例收集五(内容提供者、动画)

时间:2016-06-02 14:49:05      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

内容提供者

配置:

<provider 
    android:name="com.itheima.mycontentprovider.PersonProvider"     
    android:authorities="com.itheima.people"
    android:exported="true">

Provider:

public class PersonProvider extends ContentProvider {
    private SQLiteDatabase db;
    //创建uri匹配器
    UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
    {
        //添加匹配规则
        //arg0:主机名
        //arg1:路径
        //arg2:匹配码
        um.addURI("com.itheima.people", "person", 1);//content://com.itheima.people/person
        um.addURI("com.itheima.people", "handsome", 2);//content://com.itheima.people/handsome
        um.addURI("com.itheima.people", "person/#", 3);//content://com.itheima.people/person/10
    }

    //内容提供者创建时调用
    @Override
    public boolean onCreate() {
        MyOpenHelper oh = new MyOpenHelper(getContext());
        db = oh.getWritableDatabase();
        return false;
    }

    //values:其他应用要插的数据
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if(um.match(uri) == 1){
            db.insert("person", null, values);

            //数据库改变了,内容提供者发出通知
            //arg0:通知发到哪个uri上,注册在这个uri上的内容观察者都可以收到通知
            getContext().getContentResolver().notifyChange(uri, null);
        }
        else if(um.match(uri) == 2){
            db.insert("handsome", null, values);

            getContext().getContentResolver().notifyChange(uri, null);
        }
        else{
            throw new IllegalArgumentException("uri传错啦傻逼");
        }
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int i = 0;
        if(um.match(uri) == 1){
            i = db.delete("person", selection, selectionArgs);
        }
        else if(um.match(uri) == 2){
            i = db.delete("handsome", selection, selectionArgs);
        }
        else{
            throw new IllegalArgumentException("uri又传错啦傻逼");
        }

        return i;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int i = db.update("person", values, selection, selectionArgs);
        return i;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        if(um.match(uri) == 1){
            cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null);
        }
        else if(um.match(uri) == 2){
            cursor = db.query("handsome", projection, selection, selectionArgs, null, null, sortOrder, null);
        }
        else if(um.match(uri) == 3){
            //取出uri末尾携带的数字
            long id = ContentUris.parseId(uri);
            cursor = db.query("person", projection, "_id = ?", new String[]{"" + id}, null, null, sortOrder, null);
        }
        return cursor;
    }

    //返回通过指定uri获取的数据的mimetype
    @Override
    public String getType(Uri uri) {
        if(um.match(uri) == 1){
            return "vnd.android.cursor.dir/person";
        }
        else if(um.match(uri) == 2){
            return "vnd.android.cursor.dir/handsome";
        }
        else if(um.match(uri) == 3){
            return "vnd.android.cursor.item/person";
        }
        return null;
    }
}

OpenHelper:

public class MyOpenHelper extends SQLiteOpenHelper {
    public MyOpenHelper(Context context) {
        super(context, "people.db", null, 2);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20), money integer(10))");

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("create table handsome(_id integer primary key autoincrement, name char(10), phone char(20))");
    }
}

调用:

public void insert(View v){
    //通过内容提供者把数据插入01数据库
    //1.获取contentResolver
    ContentResolver resolver = getContentResolver();
    //2.访问内容提供者,插入数据
    ContentValues values = new ContentValues();
    values.put("name", "流氓润");
    values.put("phone", 138992);
    values.put("money", 14000);
    //arg0:指定内容提供者的主机名
    resolver.insert(Uri.parse("content://com.itheima.people/person"), values);

    values.clear();
    values.put("name", "侃哥");
    values.put("phone", 15999);
    //arg0:指定内容提供者的主机名
    resolver.insert(Uri.parse("content://com.itheima.people/handsome"), values);
   }

   public void delete(View v){
    ContentResolver resolver = getContentResolver();
    int i = resolver.delete(Uri.parse("content://com.itheima.people"), "name = ?", new String[]{"凤姐"});
    System.out.println(i);
   }

   public void update(View v){
    ContentResolver resolver = getContentResolver();
    ContentValues values = new ContentValues();
    values.put("money", 16001);
    int i = resolver.update(Uri.parse("content://com.itheima.people"), values, "name = ?", new String[]{"春晓"});
    System.out.println(i);
   }

   public void query(View v){
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person"), null, null, null, null);
    while(cursor.moveToNext()){
        String name = cursor.getString(1);
        String phone = cursor.getString(2);
        String money = cursor.getString(3);
        System.out.println(name + ";" + phone + ";" + money);
    }
   }
   public void queryOne(View v){
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person/4"), null, null, null, null);
    if(cursor.moveToNext()){
        String name = cursor.getString(1);
        String phone = cursor.getString(2);
        String money = cursor.getString(3);
        System.out.println(name + ";" + phone + ";" + money);
    }
   }

获取系统短息

   public void click1(View v){
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(Uri.parse("content://sms"), new String[]{"address", "date", "type", "body"}, null, null, null);
    while(cursor.moveToNext()){
        String address = cursor.getString(0);
        long date = cursor.getLong(1);
        int type = cursor.getInt(2);
        String body = cursor.getString(3);

        System.out.println(address + ";" + date + ";" + type + ";" + body);
    }
   }

插入系统短信

public void click(View v){
    Thread t = new Thread(){
        @Override
        public void run() {
            try {
                sleep(7000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ContentResolver resolver = getContentResolver();
            ContentValues values = new ContentValues();
            values.put("address", 95555);
            values.put("date", System.currentTimeMillis());
            values.put("type", 1);
            values.put("body", "您尾号为XXXX的招行储蓄卡收到转账1,000,000");
            resolver.insert(Uri.parse("content://sms"), values);
        }
    };
    t.start();

   }

获取系统联系人

public void click(View v){
    ContentResolver resolver = getContentResolver();

    Cursor cursor = resolver.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, 
            null, null, null);
    while(cursor.moveToNext()){
        String contactId = cursor.getString(0);
        //使用联系人id作为where条件去查询data表,查询出属于该联系人的信息
        Cursor cursorData = resolver.query(Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id = ?", 
                new String[]{contactId}, null);
//          Cursor cursorData = resolver.query(Uri.parse("content://com.android.contacts/data"), null, "raw_contact_id = ?", 
//                  new String[]{contactId}, null);

//          int count = cursorData.getColumnCount();
//          for (int i = 0; i < count; i++) {
//              System.out.println(cursorData.getColumnName(i));
//          }

        Contact contact = new Contact();
        while(cursorData.moveToNext()){
            String data1 = cursorData.getString(0);
            String mimetype = cursorData.getString(1);
//              System.out.println(data1 + ";" + mimetype);
            if("vnd.android.cursor.item/email_v2".equals(mimetype)){
                contact.setEmail(data1);
            }
            else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
                contact.setPhone(data1);
            }
            else if("vnd.android.cursor.item/name".equals(mimetype)){
                contact.setName(data1);
            }
        }
        System.out.println(contact.toString());
    }
 }

插入联系人

public void click(View v){
    ContentResolver resolver = getContentResolver();
    //先查询最新的联系人的主键,主键+1,就是要插入的联系人id
    Cursor cursor = resolver.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"_id"}, null, null, null);
    int _id = 0;
    if(cursor.moveToLast()){
        _id = cursor.getInt(0);
    }
    _id++;

    //插入联系人id
    ContentValues values = new ContentValues();
    values.put("contact_id", _id);
    resolver.insert(Uri.parse("content://com.android.contacts/raw_contacts"), values);

    //把具体联系人信息插入data表
    values.clear();
    values.put("data1", "剪刀手坤哥");
    values.put("mimetype", "vnd.android.cursor.item/name");
    values.put("raw_contact_id", _id);
    resolver.insert(Uri.parse("content://com.android.contacts/data"), values);

    values.clear();
    values.put("data1", "8899667");
    values.put("mimetype", "vnd.android.cursor.item/phone_v2");
    values.put("raw_contact_id", _id);
    resolver.insert(Uri.parse("content://com.android.contacts/data"), values);
   }

内容观察者

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //注册内容观察者,观察者就生效了,可以接受内容提供者发出的通知
    ContentResolver resolver = getContentResolver();
    //arg0:指定接收哪个内容提供者发出的通知
    resolver.registerContentObserver(Uri.parse("content://sms"), 
            true, //如果为true,以这个uri作为开头的uri上的数据改变了,该内容观察者都会收到通知
            new MyObserver(new Handler()));
   }

class MyObserver extends ContentObserver{

public MyObserver(Handler handler) {
    super(handler);
    // TODO Auto-generated constructor stub
}

@Override
public void onChange(boolean selfChange) {
    // TODO Auto-generated method stub
    super.onChange(selfChange);
    System.out.println("短信数据库改变");
    }
}

Fragment

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       showfagment01();
   }

   private void showfagment01() {
    //1.创建fragment对象
    Fragment01 fragment1 = new Fragment01();
    //2.获取fragment管理器
    FragmentManager fm = getFragmentManager();
    //3.开启事务
    FragmentTransaction ft = fm.beginTransaction();
    //4.显示fragment
    //arg0:设置把fragment显示在哪个容器中
    ft.replace(R.id.fl, fragment1);
    //5.提交
    ft.commit();
}

   public void click1(View v){
    showfagment01();
   }
   public void click2(View v){
    //1.创建fragment对象
    Fragment02 fragment2 = new Fragment02();
    //2.获取fragment管理器
    FragmentManager fm = getFragmentManager();
    //3.开启事务
    FragmentTransaction ft = fm.beginTransaction();
    //4.显示fragment
    //arg0:设置把fragment显示在哪个容器中
    ft.replace(R.id.fl, fragment2);
    //5.提交
    ft.commit();
   }
   public void click3(View v){
    //1.创建fragment对象
    Fragment03 fragment3 = new Fragment03();
    //2.获取fragment管理器
    FragmentManager fm = getFragmentManager();
    //3.开启事务
    FragmentTransaction ft = fm.beginTransaction();
    //4.显示fragment
    //arg0:设置把fragment显示在哪个容器中
    ft.replace(R.id.fl, fragment3);
    //5.提交
            ft.commit();
  }

Fragment数据传递

public void click4(View v){
    EditText et_main = (EditText) findViewById(R.id.et_main);
    String text = et_main.getText().toString();
    fragment1.setText(text);
   }

   public void setText(String text){
    TextView tv_main = (TextView) findViewById(R.id.tv_main);
    tv_main.setText(text);
  }

帧动画

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     ImageView rocketImage = (ImageView) findViewById(R.id.iv);
     //设置iv的背景图
     rocketImage.setBackgroundResource(R.drawable.plusstolensee);
     //获取iv的背景
     AnimationDrawable rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
     //开始播放
     rocketAnimation.start();
}

补间动画

private ImageView iv;
private TranslateAnimation ta;
private ScaleAnimation sa;
private AlphaAnimation aa;
private RotateAnimation ra;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv = (ImageView) findViewById(R.id.iv);
}

public void translate(View v){
    //定义位移补间动画
//      TranslateAnimation ta = new TranslateAnimation(-100, 100, -60, 60);

    ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.5f, Animation.RELATIVE_TO_SELF, 1.5f, 
            Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 2);
    //定义动画持续时间
    ta.setDuration(2000);
    //设置重复次数
    ta.setRepeatCount(1);
    //设置重复模式
    ta.setRepeatMode(Animation.REVERSE);
    //在结束位置上填充动画
    ta.setFillAfter(true);
    //播放动画
    iv.startAnimation(ta);

}

public void scale(View v){
//      ScaleAnimation sa = new ScaleAnimation(0.2f, 2, 0.2f, 2);
//      ScaleAnimation sa = new ScaleAnimation(0.2f, 2, 0.2f, 2, iv.getWidth()/2, iv.getHeight()/2);

    sa = new ScaleAnimation(0.3f, 2, 0.2f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    //定义动画持续时间
    sa.setDuration(2000);
    //设置重复次数
    sa.setRepeatCount(1);
    //设置重复模式
    sa.setRepeatMode(Animation.REVERSE);
    //在结束位置上填充动画
    sa.setFillAfter(true);
    //播放动画
    iv.startAnimation(sa);
}

public void alpha(View v){
    aa = new AlphaAnimation(1, 0.2f);
    aa.setDuration(2000);
    aa.setRepeatCount(1);
    aa.setRepeatMode(Animation.REVERSE);
    aa.setFillAfter(true);
    iv.startAnimation(aa);
}

public void rotate(View v){
//      RotateAnimation ra = new RotateAnimation(0, 720);
//      RotateAnimation ra = new RotateAnimation(0, 720, iv.getWidth()/2, iv.getHeight()/2);

    ra = new RotateAnimation(0, -720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    ra.setDuration(2000);
    ra.setRepeatCount(1);
    ra.setRepeatMode(Animation.REVERSE);
    ra.setFillAfter(true);
    iv.startAnimation(ra);
}

public void fly(View v){
    //创建动画集合
    AnimationSet set = new AnimationSet(false);
    //把动画添加至集合
    set.addAnimation(ta);
    set.addAnimation(sa);
    set.addAnimation(aa);
    set.addAnimation(ra);

    //开始播放集合
    iv.startAnimation(set);
}

属性动画

private ImageView iv;
private ObjectAnimator oa1;
private ObjectAnimator oa2;
private ObjectAnimator oa3;
private ObjectAnimator oa4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv = (ImageView) findViewById(R.id.iv);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "点我上miss", 0).show();
        }
    });
}

public void translate(View v){
//      TranslateAnimation ta = new TranslateAnimation(-100, 100, 0, 0);
//      ta.setDuration(2000);
//      ta.setFillAfter(true);
//      iv.startAnimation(ta);
    //创建属性动画师
    //arg0:要操作的对象
    //arg1:要修改的属性的名字

    oa1 = ObjectAnimator.ofFloat(iv, "translationX", 0, 70, 30, 100);
    oa1.setDuration(2000);
    oa1.setRepeatCount(1);
    oa1.setRepeatMode(ValueAnimator.REVERSE);
    oa1.start();
}

public void scale(View v){
    oa2 = ObjectAnimator.ofFloat(iv, "scaleX", 0.2f, 2, 1, 2.5f);
    oa2.setDuration(2000);
    oa2.setRepeatCount(1);
    oa2.setRepeatMode(ValueAnimator.REVERSE);
    oa2.start();
}

public void alpha(View v){
    oa3 = ObjectAnimator.ofFloat(iv, "alpha", 0.2f, 1);
    oa3.setDuration(2000);
    oa3.setRepeatCount(1);
    oa3.setRepeatMode(ValueAnimator.REVERSE);
    oa3.start();
}

public void rotate(View v){
    oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 360, 180, 720);
    oa4.setDuration(2000);
    oa4.setRepeatCount(1);
    oa4.setRepeatMode(ValueAnimator.REVERSE);
    oa4.start();
}

public void fly(View v){
    //创建动画师集合
    AnimatorSet set = new AnimatorSet();
    //排队飞
//      set.playSequentially(oa1, oa2, oa3, oa4);
    //一起飞
    set.playTogether(oa1, oa2, oa3, oa4);
    //设置属性动画师操作的对象
    set.setTarget(iv);
    set.start();
}

public void xml(View v){
    //使用动画师填充器把xml资源文件填充成属性动画对象
    Animator animator = AnimatorInflater.loadAnimator(this, R.animator.property_animation);
    animator.setTarget(iv);
    animator.start();
}

安卓小案例收集五(内容提供者、动画)

标签:

原文地址:http://blog.csdn.net/guanhang89/article/details/51542088

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!