Tôi có giao diện chính sau:
- Ta có cú pháp tổng quát:
<standard_prefix>://<authority>/<data_path>/<id>
- Ví dụ để lấy tất cả các bookmark trong trình duyệt ta dùng cú pháp:
content://browser/bookmarks
- Để lấy toàn bộ danh bạ trong điện thoại ta dùng cú pháp:
content://contacts/people
- Để lấy 1 contact theo 1 định danh nào đó:
content://contacts/people/3
- Để lấy các kết quả trả về ta cũng dùng Cursor để quản lý.
- Có 2 cách sử dụng hàm lấy kết quả ở đây:
Cách 1:
CursorLoader loader=new CursorLoader(context, uri, null, null, null, null);
Cursor c=loader.loadInBackground();
cách 2:
Cursor c = getContentResolver() .query(uri, null, null, null, null);
- Ta sẽ làm cụ thể từng chức năng trong ví dụ trên
- Bạn xem cấu trúc của bài tập này:
- Bạn xem XML Resource của màn hình chính (activity_main.xml):
android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" ><Button android:id="@+id/btnshowallcontact" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Show All Contact" /><Button android:id="@+id/btnaccesscalllog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Access The Call Log" /><Button android:id="@+id/btnmediastore" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Access Media Store" /><Button android:id="@+id/btnaccessbookmarks" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Access Bookmarks" /></LinearLayout>- Source code xử lý MainActivity.java:
package tranduythanh.com;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button btnshowallcontact;
Button btnaccesscalllog;
Button btnaccessmediastore;
Button btnaccessbookmarks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshowallcontact=(Button) findViewById(R.id.btnshowallcontact);
btnshowallcontact.setOnClickListener(this);
btnaccesscalllog=(Button) findViewById(R.id.btnaccesscalllog);
btnaccesscalllog.setOnClickListener(this);
btnaccessmediastore=(Button) findViewById(R.id.btnmediastore);
btnaccessmediastore.setOnClickListener(this);
btnaccessbookmarks=(Button) findViewById(R.id.btnaccessbookmarks);
btnaccessbookmarks.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
Intent intent=null;
if(v==btnshowallcontact)
{
intent=new Intent(this, ShowAllContactActivity.class);
startActivity(intent);
}
else if(v==btnaccesscalllog)
{
accessTheCallLog();
}
else if(v==btnaccessmediastore)
{
accessMediaStore();
}
else if(v==btnaccessbookmarks)
{
accessBookmarks();
}
}
/**
* hàm lấy danh sách lịch sử cuộc gọi
* với thời gian nhỏ hơn 30 giây và sắp xếp theo ngày gọi
*/
public void accessTheCallLog()
{
String [] projection=new String[]{
Calls.DATE,
Calls.NUMBER,
Calls.DURATION
};
Cursor c=getContentResolver().query(
CallLog.Calls.CONTENT_URI,
projection,
Calls.DURATION+"<?",new String[]{"30"},
Calls.DATE +" Asc");
c.moveToFirst();
String s="";
while(c.isAfterLast()==false){
for(int i=0;i<c.getColumnCount();i++){
s+=c.getString(i)+" - ";
}
s+="\n";
c.moveToNext();
}
c.close();
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
/**
* hàm đọc danh sách các Media trong SD CARD
*/
public void accessMediaStore()
{
String []projection={
MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.MediaColumns.DATE_ADDED,
MediaStore.MediaColumns.MIME_TYPE
};
CursorLoader loader=new CursorLoader
(this, Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
Cursor c=loader.loadInBackground();
c.moveToFirst();
String s="";
while(!c.isAfterLast()){
for(int i=0;i<c.getColumnCount();i++){
s+=c.getString(i)+" - ";
}
s+="\n";
c.moveToNext();
}
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
c.close();
}
/**
* hàm đọc danh sách Bookmark trong trình duyệt
*/
public void accessBookmarks()
{
String []projection={
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL,
};
Cursor c=getContentResolver()
.query(Browser.BOOKMARKS_URI, projection,
null, null, null);
c.moveToFirst();
String s="";
int titleIndex=c.getColumnIndex
(Browser.BookmarkColumns.TITLE);
int urlIndex=c.getColumnIndex
(Browser.BookmarkColumns.URL);
while(!c.isAfterLast())
{
s+=c.getString(titleIndex)+" - "+
c.getString(urlIndex);
c.moveToNext();
}
c.close();
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
- Source XML xử lý xem danh bạn (activity_show_all_contact.xml):
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ShowAllContactActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="@+id/btnback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
Xem tiếp P2
0 nhận xét:
Đăng nhận xét