Today follow the Dark Horse video to build an android app--Phone Dialer
Start a new Android project
The code in Activity_main_xml is as follows:
<Relativelayoutxmlns: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=". Mainactivity " > <!--text Box - <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter Number" /> <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" /></Relativelayout>
Run it:
You can see the layout problems, text overlap, simple solution is as follows:
<LinearLayoutxmlns: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 - <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter Number" /> <EditTextAndroid:id= "@+id/et_number"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <ButtonAndroid:id= "@+id/bt_call"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Dial" /></LinearLayout>
The interface looks like this:
Add the following code to the mainactivity:
Packagecom.wuyudong.dial;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //get to the corresponding component in the layout file by resource IDButton BT =(Button) Findviewbyid (R.id.bt_call); //set the monitor for the buttonBt.setonclicklistener (NewMyListener ()); } classMyListenerImplementsOnclicklistener {//This method is called when the button is clicked@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSystem.out.println ("button was clicked! "); //get to input box with resource IDEditText et =(EditText) Findviewbyid (R.id.et_number); //get the contents of the input boxString number =Et.gettext (). toString (). Trim (); //tell the system that our action is to callIntent Intent =NewIntent (); //Set Actionintent.setaction (Intent.action_call); //set who to callIntent.setdata (Uri.parse ("Tel:" +Number )); //start the call application and tell the system the intentionstartactivity (Intent); }} @Override Public BooleanOncreateoptionsmenu (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 making 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
05-26 19:43:22.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.execStartActivity ( 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.performClick (view.java:4240)
05-26 19:43:22.015:e/androidruntime (2042): at Android.view.view$performclick.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)
Throws a security exception prompting for no permissions
The solution is as follows:
Open Androidmanifest.xml File
Click Add to select the users Permission
Right drop-down single choice: Android.permission.CALL_PHONE
Ctrl+s
Re-run, fix it.
Android Combat-Phone Dialer