Android basic-dialing device and android dialing device
1. layout file:
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.itheima.callphone.MainActivity" > <EditText android:id="@+id/et_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/bt_dial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/et_number" android:text="@string/_dial" /></RelativeLayout>
2. MainActivity. java
Package com. itheima. callphone; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. text. textUtils; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends ActionBarActivity implements OnClickListener {// enter the phone number's text box private EditText et_number; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the number text input box et_number = (EditText) findViewById (R. id. et_number); // obtain the dialing Button bt_dial = (Button) findViewById (R. id. bt_dial); bt_dial.setOnClickListener (this);} // click events for various controls @ Override public void onClick (View v) {switch (v. getId () {case R. id. bt_dial: callPhone (); break; default: break; }}// implement the dialing operation private void callPhone () {// obtain the number in the number input box String number = et_number.getText (). toString (). trim (); // perform non-null judgment if (TextUtils. isEmpty (number) {// prompt Toast to the user. makeText (MainActivity. this, "the number cannot be blank", Toast. LENGTH_LONG ). show (); return;
} // Enable the dialing service Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + number); startActivity (intent );}}
3. Common Errors
Error description:
Java. lang. securityException: Permission Denial: starting Intent {act = android. intent. action. CALL dat = tel: xxx-xxxx cmp = com. android. phone /. outgoingCallBroadcaster} from ProcessRecord {b76dbe10 1322: com. itheima. callphone/10032} (pid = 1322, uid = 10032) requires android. permission. CALL_PHONE
Cause: You have no dialing permission for the application. The method is shown in.