技术开发 频道

Hello, OPhone!

  【IT168  技术文档】下面将会举一个例子,创建一个简单的OPhone应用程序,输出所有的呼叫记录,这个例子使用了OPhone的Local Search API。

  创建OPhone工程

  在开始创建第一个工程之前,必需要先配置好 Eclipse环境(可以在Windows或者Linux上安装Eclipse),如果没有Eclipse,可以先到Eclipse的官方网站上去下载Eclipse IDE 。有了Eclipse,还要确认Eclipse中安装了ADT。安装ADT的过程,可以参考 安装Eclipse 插件。

  1.创建OPhone工程

  打开Eclipse, 选择 “File > New, 选择“Android Project”选项

  2.设置Project属性

  打开“New Android Project”对话框,输入Project名,设置Project属性。具体参照下图: Contents部分和普通的Eclipse工程创建一样。Properties部分需要填写java代码的package名,还要设置Activity名, 对于OPhone应用程序来说, Activity是程序的入口。  

  3.添加OPhone 库支持

  创建Android工程后,在Eclipse IDE界面左侧的“Package Explorer”中选择刚才创建的工程,点击右键或者打开“Project”菜单,选择“Properties”。在弹出的属性设置窗口中选取“Java Build Path”,你将会看到如下窗口,选中“Libraries”选项页:  

  

  点击“Add Library...”按钮

  

  选择 “User Library”, 然后点击 “Next >”

  如下图所示的对话框,选中OPhone ,如果你发现没有OPhone选项,可以点击“User Libraries...” ,配置OPhone库,具体细节,请参考:在Eclipse IDE中添加OPhone库。

  

  选中 “OPhone”后, 点击 “Finish”。工程属性对话框会显示如下:.

  点击“OK”后,一个OPhone的Project就算创建完成了。你会发现你的工程内部有一个java文件,名字叫HelloOPhone,源代码如下:

    public class HelloOPhone extends Activity {
        
/** Called when the activity is first created. */
        @Override
        
public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
        }
    }

  编写代码

  接下来我们将修改这个自动生成的源文件,去调用OPhone API:

  示例代码要实现的功能如下::

  1. 在屏幕上,创建一个可以滚动显示的文本区域
  2. 调用OPhone的API
  3. 输出呼叫记录到文本区域中,这些记录是通过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>
  

  创建一个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();
    }
}
  

  在上面的代码中,通过调用LocalSearch API来查询电话记录信息,并把查询结果保存在字符串searchResult之中。

  输出结果

  下面的代码把查询结果显示在TextView之中:  

package oms.hello;

import ...
import android.widget.TextView;

public class HelloOPhone extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle icicle) {

        ...

        
// step 3: output the result to the TextView
        
if (searchResult != null) {
            TextView tv
= (TextView) findViewById(R.id.textview);
            tv.setText(searchResult);
        }
    }
}
  

  

  运行HelloOPhone程序

  选中HelloOPhone工程,点击右键,在弹出菜单中选择“Run as”,最后点击“Android Application”来运行程序。

 

  之后,HelloOPhone应用程序会被部署到设备(或者模拟器)上,然后自动运行,运行结果如下:

0
相关文章