编写代码
接下来我们将修改这个自动生成的源文件,去调用OPhone API:
示例代码要实现的功能如下::
- 在屏幕上,创建一个可以滚动显示的文本区域
- 调用OPhone的API
- 输出呼叫记录到文本区域中,这些记录是通过OPhone API的调用获得的
编辑XML文件,创建UI
打开main.xml,该文件的路径是: res/layout/main.xml。在Eclipse下,可以用 “Android Layout Editor”来编辑XML文件,修改后的XML内容如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
创建一个TextView控件,指定控件的id 为 “textview”。设置TextView的属性值, “layout_height”的值为“wrap_content”。
编辑工程中的Java文件,调用LocalSearch的API
接下来编辑java文件,打开ophone/hello/HelloOPhone.java,添加代码,调用LocalSearch API,修改后的代码如下:
package oms.hello;
import ...
import oms.servo.search.SearchProvider;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class HelloOPhone extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
...
// step 2: call OPhone API(LocalSearch)
String searchSelection = "type:" + SearchProvider.TYPE_CALL;
String searchResult = localSearch(searchSelection);
}
public String localSearch(String searchSelection) {
// search for SMS
Uri uri = Uri.parse(SearchProvider.CONTENT_URI);
Cursor cursor = getContentResolver().query(uri, null, searchSelection,
null, null);
StringBuffer result = new StringBuffer();
result.append("#id #calltype #title #time(#duration)\n");
// print result out
while (cursor.moveToNext()) {
// Use cursor.respond function to get the data.
Bundle extras = new Bundle();
extras = cursor.respond(extras);
// Extract the data from search result
String id = extras.getString(SearchProvider.FIELD_ID);
String calltype = extras.getString(SearchProvider.FIELD_CALL_TYPE);
String title = extras.getString(SearchProvider.FIELD_TITLE);
long time = Long.parseLong(extras.getString(SearchProvider.FIELD_TIME));
int duration = Integer.parseInt(extras.getString(SearchProvider.FIELD_CALL_DURATION));
result.append("\n").append(id)
.append("\n[").append(calltype).append("]")
.append("\t").append(title)
.append("\t").append(new Date(time).toString())
.append("(").append(duration).append(")")
.append("\n");
}
cursor.close();
return result.toString();
}
}
import ...
import oms.servo.search.SearchProvider;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class HelloOPhone extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
...
// step 2: call OPhone API(LocalSearch)
String searchSelection = "type:" + SearchProvider.TYPE_CALL;
String searchResult = localSearch(searchSelection);
}
public String localSearch(String searchSelection) {
// search for SMS
Uri uri = Uri.parse(SearchProvider.CONTENT_URI);
Cursor cursor = getContentResolver().query(uri, null, searchSelection,
null, null);
StringBuffer result = new StringBuffer();
result.append("#id #calltype #title #time(#duration)\n");
// print result out
while (cursor.moveToNext()) {
// Use cursor.respond function to get the data.
Bundle extras = new Bundle();
extras = cursor.respond(extras);
// Extract the data from search result
String id = extras.getString(SearchProvider.FIELD_ID);
String calltype = extras.getString(SearchProvider.FIELD_CALL_TYPE);
String title = extras.getString(SearchProvider.FIELD_TITLE);
long time = Long.parseLong(extras.getString(SearchProvider.FIELD_TIME));
int duration = Integer.parseInt(extras.getString(SearchProvider.FIELD_CALL_DURATION));
result.append("\n").append(id)
.append("\n[").append(calltype).append("]")
.append("\t").append(title)
.append("\t").append(new Date(time).toString())
.append("(").append(duration).append(")")
.append("\n");
}
cursor.close();
return result.toString();
}
}
在上面的代码中,通过调用LocalSearch API来查询电话记录信息,并把查询结果保存在字符串searchResult之中。