模拟android拨号器的实现
AndroidManifest.xml清单文件:
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.class3g.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".PhoneActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml布局文件:
<?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/input_info"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone_number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dial_btn"
android:text="@string/dial_caotion"
/>
</LinearLayout>
strings.xml:
<?xml version="1.0"encoding="utf-8"?>
<resources>
<string name="hello">Hello World,PhoneActivity!</string>
<string name="app_name">Phone</string>
<string name="input_info">请输入号码</string>
<string name="dial_caotion">拨打</string>
</resources>
PhoneActivity.java类:
packagecn.class3g.activity;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.view.View;
import android.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
public classPhoneActivity extends Activity {
EditText numberEt;
Button dialBtn;
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
dialBtn.setOnClickListener(newOnClickListener() {
publicvoid onClick(View v) {
//调用系统的拨号服务实现电话拨打功能
Stringphone_number = numberEt.getText().toString();
phone_number= phone_number.trim();
if(phone_number!=null&&!phone_number.equals("")){
//封装一个拨打电话的intent,并且将电话号码包装成一个Uri对象传入
Intentintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));
PhoneActivity.this.startActivity(intent);
}
}
});
}
public void findViews(){
numberEt= (EditText) this.findViewById(R.id.phone_number);
dialBtn= (Button) this.findViewById(R.id.dial_btn);
}
}