package com.paad.contentproviders;
import java.util.Calendar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Ch08_ContentProvidersActivity extends Activity {
TextView resultTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultTextView = (TextView)findViewById(R.id.resultTextView);
Button mediaStoreButton = (Button)findViewById(R.id.button1);
mediaStoreButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AsyncTask<Void, Void, StringBuilder> task = new AsyncTask<Void, Void, StringBuilder>() {
@Override
protected StringBuilder doInBackground(Void... params) {
String[] result = getSongListing();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++)
sb.append(result[i]);
return sb;
}
@Override
protected void onPostExecute(StringBuilder sb) {
resultTextView.setText(sb.toString());
}
};
task.execute();
}
});
Button contactsButton = (Button)findViewById(R.id.button2);
contactsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AsyncTask<Void, Void, StringBuilder> task = new AsyncTask<Void, Void, StringBuilder>() {
@Override
protected StringBuilder doInBackground(Void... params) {
String[] result = getNames();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++)
sb.append(result[i]);
return sb;
}
@Override
protected void onPostExecute(StringBuilder sb) {
resultTextView.setText(sb.toString());
}
};
task.execute();
}
});
Button contactButton2 = (Button)findViewById(R.id.button3);
contactButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AsyncTask<Void, Void, StringBuilder> task = new AsyncTask<Void, Void, StringBuilder>() {
@Override
protected StringBuilder doInBackground(Void... params) {
String[] result = getNameAndNumber();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++)
sb.append(result[i]);
return sb;
}
@Override
protected void onPostExecute(StringBuilder sb) {
resultTextView.setText(sb.toString());
}
};
task.execute();
}
});
Button reverseLookupButton = (Button)findViewById(R.id.button4);
reverseLookupButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
return performCallerId();
}
@Override
protected void onPostExecute(String result) {
resultTextView.setText(result);
}
};
task.execute();
}
});
Button pickContactButton = (Button)findViewById(R.id.button5);
pickContactButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pickContact();
}
});
Button insertContactButton = (Button)findViewById(R.id.button6);
insertContactButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
insertContactWithIntent();
}
});
Button queryEventsButton = (Button)findViewById(R.id.button7);
queryEventsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AsyncTask<Void, Void, StringBuilder> task = new AsyncTask<Void, Void, StringBuilder>() {
@Override
protected StringBuilder doInBackground(Void... params) {
String[] result = queryCalendar();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++)
sb.append(result[i]);
return sb;
}
@Override
protected void onPostExecute(StringBuilder sb) {
resultTextView.setText(sb.toString());
}
};
task.execute();
}
});
Button addEventButton = (Button)findViewById(R.id.button8);
addEventButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
insertNewEventIntoCalendar();
}
});
Button editEventButton = (Button)findViewById(R.id.button9);
editEventButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editCalendarEvent();
}
});
Button viewTimeButton = (Button)findViewById(R.id.button10);
viewTimeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
displayCalendarEvent();
}
});
}
private String[] getSongListing() {
/**
* Listing 8-35: Accessing the Media Store Content Provider
*/
// Get a Cursor over every piece of audio on the external volume,
// extracting the song title and album name.
String[] projection = new String[] {
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.AudioColumns.TITLE
};
Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor =
getContentResolver().query(contentUri, projection,
null, null, null);
// Get the index of the columns we need.
int albumIdx =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.ALBUM);
int titleIdx =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE);
// Create an array to store the result set.
String[] result = new String[cursor.getCount()];
// Iterate over the Cursor, extracting each album name and song title.
while (cursor.moveToNext()) {
// Extract the song title.
String title = cursor.getString(titleIdx);
// Extract the album name.
String album = cursor.getString(albumIdx);
result[cursor.getPosition()] = title + " (" + album + ")";
}
// Close the Cursor.
cursor.close();
//
return result;
}
private String[] getNames() {
/**
* Listing 8-36: Accessing the Contacts Contract Contact Content Provider
*/
// Create a projection that limits the result Cursor
// to the required columns.
String[] projection = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
// Get a Cursor over the Contacts Provider.
Cursor cursor =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, null);
// Get the index of the columns.
int nameIdx =
cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int idIdx =
cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
// Initialize the result set.
String[] result = new String[cursor.getCount()];
// Iterate over the result Cursor.
while(cursor.moveToNext()) {
// Extra
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android4高级编程全书完成源码 (2000个子文件)
IEarthquakeService.aidl 158B
IEarthquakeService.aidl 158B
IEarthquakeService.aidl 158B
IEarthquakeService.aidl 158B
quake.aidl 47B
quake.aidl 47B
quake.aidl 47B
quake.aidl 47B
jarlist.cache 119B
jarlist.cache 119B
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
EarthquakeUpdateService.class 12KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- sugarqian2014-06-30很全,谢谢
- qianrushi1102014-07-29全书的代码详细,能获得这样的代码就算不错了,虽然你可以在wrox.com下载
- lgj19912015-11-05全书的代码详细,有帮助
- daiqiongyao892014-09-20高级编程,很好的
小伙在纠结
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功