ContentProvider获取手机信息,简单修改数据库数据

来源:互联网 发布:网络调查 编辑:程序博客网 时间:2024/06/08 04:06
public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private TextView empty_tv;


    private String path = "content://sms";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        empty_tv = new TextView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        empty_tv.setText("当前listView为空");
        empty_tv.setLayoutParams(params);


        listView.setEmptyView(empty_tv);


        // 获取ContentRecolver 实例化对象
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(Uri.parse(path), null, null, null, null);


        MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(adapter);
    }


    class MyCursorAdapter extends CursorAdapter {


        public MyCursorAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
            // TODO Auto-generated constructor stub
        }




        /*
         * 第一个参数:  当前Item的 View(ViewGroup)
         * 第三个参数  就是  当前Cursor
         *
         */
        @Override
        public void bindView(View view, Context context, Cursor c) {


            TextView address_tv = (TextView) view.findViewById(R.id.address_tv);
            TextView body_tv = (TextView) view.findViewById(R.id.body_tv);
            TextView type_tv = (TextView) view.findViewById(R.id.type_tv);
            // 电话号码
            String number = c.getString(c.getColumnIndex("address"));
            // 消息发送的内容
            String body = c.getString(c.getColumnIndex("body"));
            // 消息的类型
            String type = c.getString(c.getColumnIndex("type"));


            address_tv.setText(number);
            body_tv.setText(body);


            // 进行判断 是接收到的短信 还是发送出去的短信
            if (type.equals("1")) {
                type_tv.setText("接收");
            } else if (type.equals("2")) {
                type_tv.setText("发送");
            }


        }




         //直接将每个条目的View  返回即可
        @Override
        public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
            return LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, null);
        }


    }


}

//在清单文件中添加的权限

<uses-permission android:name="android.permission.READ_SMS"/>




//清单文件注册的provider

<provider android:name=".provider.MyContentProvider"
                  android:authorities="alice.bw.com.day16customcontentprovider"
                  android:exported="true"></provider>
    </application>

//创建数据库

/**
 * 创建数据库
 * @author Administrator
 *
 */
public class MySqliteHelper extends SQLiteOpenHelper {


private static final String DB_NAME = "student";
private static int DB_VERSON = 1;


public MySqliteHelper(Context context) {
super(context, DB_NAME, null, DB_VERSON);
}


//创建表
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create  table  student(_id integer primary key autoincrement,name text,age text)";
db.execSQL(sql);


}


        //更新表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}



//修改数据库的类

public class MyContentProvider extends ContentProvider{
// 定义Uri
String path = "content://alice.bw.com.day16customcontentprovider/student";


SQLiteDatabase db;


//删除一条数据
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int result = db.delete("student", selection, selectionArgs);
return result;
}


@Override
public String getType(Uri arg0) {
return null;
}


//插入一条数据
@Override
public Uri insert(Uri uri, ContentValues values) {
long insert = db.insert("student", null, values);
Uri resultUri = ContentUris.withAppendedId(Uri.parse(path), insert);
return resultUri;
}


//实例化SQLiteDatabase
@Override
public boolean onCreate() {
MySqliteHelper helper = new MySqliteHelper(getContext());
db = helper.getReadableDatabase();
return false;
}


//查询
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor c = db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
return c;
}


//修改一条数据
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
}


}








原创粉丝点击