android 拨打电话小功能

1.其实就是对Intent 的ACTION进行参数设置。

在manifest中药设置打电话的权限:

1  <uses-permission android:name="android.permission.CALL_PHONE" />

xml:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <EditText
 9         android:id="@+id/phoneNumber"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:inputType="phone" />
13 
14     <Button
15         android:id="@+id/btn_call"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="打电话" />
19 
20 </LinearLayout>

Activity:

 1 public class MainActivity extends Activity {
 2 
 3     private EditText Number;
 4 
 5     private Button btn;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         Number = (EditText) findViewById(R.id.phoneNumber);
12         btn = (Button) findViewById(R.id.btn_call);
13         btn.setOnClickListener(new OnClickListener() {
14 
15             @Override
16             public void onClick(View v) {
17                 if (Number.getText().toString().trim().equals("")) {
18                     Number.setError("电话号码不能为空");
19                 } else {
20 
21                     Intent i = new Intent("android.intent.action.CALL", Uri
22                             .parse("tel:" + Number.getText().toString().trim()));
23                     startActivity(i);
24                 }
25 
26             }
27         });
28 
29     }
30 }

 

posted @ 2014-08-25 23:04  perfect亮  阅读(254)  评论(0编辑  收藏  举报