Android——ContentProvide 内容提供者+四大组件之三
2016-09-12 21:30
513 查看
Android提供的用于不同应用共享数据API
Android系统提供的 ContentProvide
1Applications 提供已安装应用程序信息
Browser 浏览器
CallLog 储存通话记录
.........................
[java] view
plain copy
package c.example.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 c.example.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 MyListener());
mp3.setOnClickListener(new MyListener());
mp4.setOnClickListener(new MyListener());
}
public class MyListener implements View.OnClickListener{
@Override
public void onClick(View view) {
int id=view.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[]{"50000"},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(3)+"\n");
}
show.setText(sbd.toString());
}
}
}
}
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="c.example.jreduch09.contentprovider.ContentActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/img"
android:text="扫描图库"
android:textSize="25sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mp3"
android:text="扫描音频"
android:textSize="25sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mp4"
android:text="扫描视频"
android:textSize="25sp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show"
/>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Android系统提供的 ContentProvide
1Applications 提供已安装应用程序信息
Browser 浏览器
CallLog 储存通话记录
.........................
[java] view
plain copy
package c.example.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 c.example.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 MyListener());
mp3.setOnClickListener(new MyListener());
mp4.setOnClickListener(new MyListener());
}
public class MyListener implements View.OnClickListener{
@Override
public void onClick(View view) {
int id=view.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[]{"50000"},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(3)+"\n");
}
show.setText(sbd.toString());
}
}
}
}
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="c.example.jreduch09.contentprovider.ContentActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/img"
android:text="扫描图库"
android:textSize="25sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mp3"
android:text="扫描音频"
android:textSize="25sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mp4"
android:text="扫描视频"
android:textSize="25sp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show"
/>
</ScrollView>
</LinearLayout>
</RelativeLayout>
相关文章推荐
- android LinearLayout 安卓无忧第一篇
- AndroidStudio自定义注释
- android LinearLayout 安卓无忧第一篇
- AndroidStudio快捷键和使用技巧
- Android Studio SVN的使用
- Android Rxjava的from方法的基本用法
- Android开发之基于MPAndroidChart实现股票K线图
- Android中如何一次性finish掉以前打开的所有的activity
- android各种适配器的用法
- Android中的shape中的属性大全
- Android学习之路
- Android中shape属性详解
- Android仿超级课程表的课程界面设计
- AndroidStudio指定git的忽略文件
- Android Service完全解析
- Android api level对照表
- Android布局优化的一些小技巧
- Android美女拼图游戏
- Android Loader 异步加载数据
- andorid使用ksoap2进行WebService通信