Android四大组件之一:ContentProvider(内容提供者)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android四大组件之一:ContentProvider(内容提供者)相关的知识,希望对你有一定的参考价值。
android中还提供了名为ContentProvider(内容提供者),可以向其他应用提供数据,但不常用,除非是同一公司开发的App,可以向不同应用提供数据。虽然为Android的四大组件之一,但用法并不复杂。
建立一个Activity,设置三个Button,分别用于手机相册,Mp3,Mp4的数据的获取。在这里用的是内部类监听方式,不要忘了注册监听。
package com.example.administrator.jreduch09.contentprovider;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.administrator.jreduch09.R;
public class ContentActivity extends AppCompatActivity {
private Button img, mp3, mp4;
private TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
img = (Button) findViewById(R.id.img);
mp3 = (Button) findViewById(R.id.mp3);
mp4 = (Button) findViewById(R.id.mp4);
show = (TextView) findViewById(R.id.show);
img.setOnClickListener(new MyListerner());
mp3.setOnClickListener(new MyListerner());
mp4.setOnClickListener(new MyListerner());
}
public class MyListerner implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.img) {
String str[] = {MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI
, str,
null, null, null);
StringBuilder sbd = new StringBuilder();
while (cursor.moveToNext()) {
sbd.append(cursor.getString(0) + ":");
sbd.append(cursor.getString(1) + ":");
sbd.append(cursor.getString(2) + "\n");
}
show.setText(sbd.toString());
} else if (id == R.id.mp3) {
String str[] = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.ARTIST,//作者
MediaStore.Audio.Media.DURATION,//时常
MediaStore.Audio.Media.ALBUM};//专辑
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
str,
"duration>?",
new String[]{"3000"}, null);
StringBuilder sbd = new StringBuilder();
while (cursor.moveToNext()) {
sbd.append(cursor.getString(0) + ":");
sbd.append(cursor.getString(1) + ":");
sbd.append(cursor.getString(2) + ":");
sbd.append(cursor.getString(3) + ":");
sbd.append(cursor.getString(4) + ":");
sbd.append(cursor.getString(5) + ":");
sbd.append(cursor.getString(6) + "\n");
}
show.setText(sbd.toString());
} else if (id == R.id.mp4) {
String str[] = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.SIZE};
Cursor cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
str,
null, null, null);
StringBuilder sbd = new StringBuilder();
while (cursor.moveToNext()) {
sbd.append(cursor.getString(0) + ":");
sbd.append(cursor.getString(1) + ":");
sbd.append(cursor.getString(2) + ":");
sbd.append(cursor.getString(1) + "\n");
}
show.setText(sbd.toString());
}
}
}
}
以上是关于Android四大组件之一:ContentProvider(内容提供者)的主要内容,如果未能解决你的问题,请参考以下文章