Android学习笔记(三) 之模拟打电话
新建一个Android工程,基于Anrdoid2.2模拟器创建
然后完善string.xml文件
[html]
<span style="font-size:18px;color:#3366ff;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SharpPhoneActivity!</string>
<string name="app_name">SharpPhone</string>
<string name="mobile">请输入手机号</string>
<string name="button">拨打此号</string>
</resources></span>
然后是完善界面 修改main.xml
[html]
<span style="font-size:18px;color:#3366ff;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phoneno" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout></span>
接着需要在Activity类处理信息
[java]
<span style="font-size:18px;color:#3366ff;">package com.sharpanroid.phone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SharpPhoneActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt=(Button)this.findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText phoneText=(EditText)findViewById(R.id.phoneno);
/**
* 获取手机号
*/
String phoneno=phoneText.getText().toString();
if(null!=phoneno&&!"".equals(phoneno.trim())){
/**
* 数据通过Uri包封装 ,创建一个拨号意图
*/
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneno));
startActivity(intent);
}
}
});
}
}</span>
由于调用了打电话的服务,需要在AndroidManifest.xml 文件中进行引用
[java]
<span style="font-size:18px;color:#3366ff;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sharpanroid.phone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharpPhoneActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
&nb
补充:移动开发 , Android ,