Ophone的程序:Hello, OPhone!

Hello, OPhone!

下面将会举一个例子,创建一个简单的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,源代码如下:

  1. publicclassHelloOPhoneextendsActivity{
  2. /**Calledwhentheactivityisfirstcreated.*/
  3. @Override
  4. publicvoidonCreate(Bundleicicle){
  5. super.onCreate(icicle);
  6. setContentView(R.layout.main);
  7. }
  8. }
	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内容如下:

    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="fill_parent"
    4. android:layout_height="fill_parent">
    5. <TextViewandroid:id="@+id/textview"
    6. android:layout_width="fill_parent"
    7. android:layout_height="wrap_content"/>
    8. </ScrollView>
    	<?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,修改后的代码如下:

    1. packageoms.hello;
    2. import...
    3. importoms.servo.search.SearchProvider;
    4. importandroid.database.Cursor;
    5. importandroid.net.Uri;
    6. importandroid.os.Bundle;
    7. publicclassHelloOPhoneextendsActivity{
    8. /**Calledwhentheactivityisfirstcreated.*/
    9. @Override
    10. publicvoidonCreate(Bundleicicle){
    11. ...
    12. //step2:callOPhoneAPI(LocalSearch)
    13. StringsearchSelection="type:"+SearchProvider.TYPE_CALL;
    14. StringsearchResult=localSearch(searchSelection);
    15. }
    16. publicStringlocalSearch(StringsearchSelection){
    17. //searchforSMS
    18. Uriuri=Uri.parse(SearchProvider.CONTENT_URI);
    19. Cursorcursor=getContentResolver().query(uri,null,searchSelection,
    20. null,null);
    21. StringBufferresult=newStringBuffer();
    22. result.append("#id#calltype#title#time(#duration)/n");
    23. //printresultout
    24. while(cursor.moveToNext()){
    25. //Usecursor.respondfunctiontogetthedata.
    26. Bundleextras=newBundle();
    27. extras=cursor.respond(extras);
    28. //Extractthedatafromsearchresult
    29. Stringid=extras.getString(SearchProvider.FIELD_ID);
    30. Stringcalltype=extras.getString(SearchProvider.FIELD_CALL_TYPE);
    31. Stringtitle=extras.getString(SearchProvider.FIELD_TITLE);
    32. longtime=Long.parseLong(extras.getString(SearchProvider.FIELD_TIME));
    33. intduration=Integer.parseInt(extras.getString(SearchProvider.FIELD_CALL_DURATION));
    34. result.append("/n").append(id)
    35. .append("/n[").append(calltype).append("]")
    36. .append("/t").append(title)
    37. .append("/t").append(newDate(time).toString())
    38. .append("(").append(duration).append(")")
    39. .append("/n");
    40. }
    41. cursor.close();
    42. returnresult.toString();
    43. }
    44. }
    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之中:

    1. packageoms.hello;
    2. import...
    3. importandroid.widget.TextView;
    4. publicclassHelloOPhoneextendsActivity{
    5. /**Calledwhentheactivityisfirstcreated.*/
    6. @Override
    7. publicvoidonCreate(Bundleicicle){
    8. ...
    9. //step3:outputtheresulttotheTextView
    10. if(searchResult!=null){
    11. TextViewtv=(TextView)findViewById(R.id.textview);
    12. tv.setText(searchResult);
    13. }
    14. }
    15. }
    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应用程序会被部署到设备(或者模拟器)上,然后自动运行,运行结果如下:


Copyright © China Mobile 2009

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值