Android practice-dialing device and android dialing device
Today, I created an android app-Phone Dialer with the dark horse video.
Create an android Project
The code in activity_main_xml is as follows:
<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context =". MainActivity "> <! -- Text box --> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Enter the number"/> <EditText android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </RelativeLayout>
Run:
You can see that the layout is faulty and the text is overlapped. The simple solution is as follows:
<LinearLayout 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" tools: context = ". mainActivity "android: orientation =" vertical "> <! -- Text box --> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Enter the number"/> <EditText android: id = "@ + id/et_number" android: layout_width = "match_parent" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/bt_call" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "dial"/> </LinearLayout>
The interface is as follows:
Add the following code to MainActivity:
Package com. wuyudong. dial; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCr Eate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the corresponding component Button bt = (Button) findViewById (R. id. bt_call); // configure listening bt for the button. setOnClickListener (new MyListener ();} class MyListener implements OnClickListener {// when the button is clicked, this method will be called @ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("the button is clicked! "); // Obtain the EditText et = (EditText) findViewById (R. id. et_number); // get the content of the input box String number = et. getText (). toString (). trim (); // tell the system that our action is to call Intent intent = new Intent (); // set the action intent. setAction (Intent. ACTION_CALL); // sets the name of the user to intent. setData (Uri. parse ("tel:" + number); // start the call application and tell the system startActivity (intent); }}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
After a call, the following exception occurs:
05-26 19:43:02. 155: D/dalvikvm (2042): Not late-enabling CheckJNI (already on)
05-26 19:43:02. 775: D/gralloc_goldfish (2042): Emulator without GPU emulation detected.
05-26 19:43:22. 015: I/System. out (2042): the button is clicked!
05-26 19:43:22. 015: D/AndroidRuntime (2042): Shutting down VM
05-26 19:43:22. 015: W/dalvikvm (2042): threadid = 1: thread exiting with uncaught exception (group = 0xb0e8d648)
05-26 19:43:22. 015: E/AndroidRuntime (2042): fatal exception: main
19:43:22 05-26. 015: E/AndroidRuntime (2042): java. lang. securityException: Permission Denial: starting Intent {act = android. intent. action. CALL dat = tel: xxx cmp = com. android. phone /. outgoingCallBroadcaster} from ProcessRecord {b1362a50 2042: com. wuyudong. dial/u0a10047} (pid = 2042, uid = 10047) requires android. permission. CALL_PHONE
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. OS. Parcel. readException (Parcel. java: 1431)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. OS. Parcel. readException (Parcel. java: 1385)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. ActivityManagerProxy. startActivity (ActivityManagerNative. java: 1947)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android.app.Instrumentation.exe cStartActivity (Instrumentation. java: 1419)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. Activity. startActivityForResult (Activity. java: 3390)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. Activity. startActivityForResult (Activity. java: 3351)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. Activity. startActivity (Activity. java: 3587)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. Activity. startActivity (Activity. java: 3555)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at com. wuyudong. dial. MainActivity $ MyListener. onClick (MainActivity. java: 44)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. view. View. sharemclick (View. java: 4240)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. view. View $ export mclick. run (View. java: 17721)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. OS. Handler. handleCallback (Handler. java: 730)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. OS. Handler. dispatchMessage (Handler. java: 92)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. OS. Looper. loop (Looper. java: 137)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at android. app. ActivityThread. main (ActivityThread. java: 5103)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at java. lang. reflect. Method. invokeNative (Native Method)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at java. lang. reflect. Method. invoke (Method. java: 525)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at com. android. internal. OS. ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 737)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at com. android. internal. OS. ZygoteInit. main (ZygoteInit. java: 553)
05-26 19:43:22. 015: E/AndroidRuntime (2042): at dalvik. system. NativeStart. main (Native Method)
A security exception is thrown, prompting you to have no permissions.
The solution is as follows:
Open the AndroidManifest. xml file
Click Add and select Users Permission.
Select android. permission. CALL_PHONE from the drop-down list on the right.
Ctrl + S
Run it again.