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. public class HelloOPhone extends Activity {   
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle icicle) {   
  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. <?xml version="1.0" encoding="utf-8"?>  
    2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.     <TextView android:id="@+id/textview"  
    6.         android:layout_width="fill_parent"  
    7.         android:layout_height="wrap_content" />  
    8. </ScrollView>  
    9.    

    创建一个TextView控件,指定控件的id 为 “textview”。设置TextView的属性值, “layout_height”的值为“wrap_content”。

  • 编辑工程中的Java文件,调用LocalSearch的API

    接下来编辑java文件,打开ophone/hello/HelloOPhone.java,添加代码,调用LocalSearch API,修改后的代码如下:

    1. package oms.hello;   
    2.   
    3. import ...   
    4. import oms.servo.search.SearchProvider;   
    5. import android.database.Cursor;   
    6. import android.net.Uri;   
    7. import android.os.Bundle;   
    8.   
    9. public class HelloOPhone extends Activity {   
    10.     /** Called when the activity is first created. */  
    11.     @Override  
    12.     public void onCreate(Bundle icicle) {   
    13.   
    14.         ...   
    15.            
    16.         // step 2: call OPhone API(LocalSearch)   
    17.         String searchSelection = "type:" + SearchProvider.TYPE_CALL;   
    18.         String searchResult = localSearch(searchSelection);   
    19.     }   
    20.   
    21.     public String localSearch(String searchSelection) {   
    22.         // search for SMS   
    23.         Uri uri = Uri.parse(SearchProvider.CONTENT_URI);   
    24.         Cursor cursor = getContentResolver().query(uri, null, searchSelection,   
    25.                 nullnull);   
    26.   
    27.         StringBuffer result = new StringBuffer();   
    28.         result.append("#id   #calltype   #title   #time(#duration)/n");   
    29.         // print result out   
    30.         while (cursor.moveToNext()) {   
    31.             // Use cursor.respond function to get the data.   
    32.             Bundle extras = new Bundle();   
    33.             extras = cursor.respond(extras);   
    34.             // Extract the data from search result   
    35.             String id = extras.getString(SearchProvider.FIELD_ID);   
    36.             String calltype = extras.getString(SearchProvider.FIELD_CALL_TYPE);   
    37.             String title = extras.getString(SearchProvider.FIELD_TITLE);   
    38.             long time = Long.parseLong(extras.getString(SearchProvider.FIELD_TIME));   
    39.             int duration = Integer.parseInt(extras.getString(SearchProvider.FIELD_CALL_DURATION));   
    40.             result.append("/n").append(id)   
    41.                 .append("/n[").append(calltype).append("]")   
    42.                 .append("/t").append(title)   
    43.                 .append("/t").append(new Date(time).toString())   
    44.                 .append("(").append(duration).append(")")   
    45.                 .append("/n");   
    46.         }   
    47.   
    48.         cursor.close();   
    49.         return result.toString();   
    50.     }   
    51. }   
    52.     
    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. package oms.hello;   
    2.   
    3. import ...   
    4. import android.widget.TextView;   
    5.   
    6. public class HelloOPhone extends Activity {   
    7.     /** Called when the activity is first created. */  
    8.     @Override  
    9.     public void onCreate(Bundle icicle) {   
    10.   
    11.         ...   
    12.   
    13.         // step 3: output the result to the TextView   
    14.         if (searchResult != null) {   
    15.             TextView tv = (TextView) findViewById(R.id.textview);   
    16.             tv.setText(searchResult);   
    17.         }   
    18.     }   
    19. }   
    20.     
    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、付费专栏及课程。

余额充值