Android already has a strong dominance in the mobile phone market and attracts a growing number of enthusiasts. Android's learning is getting more and more hot. However, the registration fee is indeed prohibitive for most people
First, new project Callphone
1.1, the establishment of the project
Second, set the interface and project name
2.1, change the name of the project
Res/values Strings.xml Change the app_name phone dialing device
String.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "app_name" > Phone dialing device </ string>
<string name= "action_settings" >Settings</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Dail" > Dial call </string>
<string name= "Defaul_ Prop "> Please enter phone number </string>
</resources>
2.2, set the file box and key
<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= ". Mainactivity "> <edittext android:id=" @+id/et_number "android:layout_width=" Match_parent "Android:layo" ut_height= "Wrap_content" android:inputtype= "Phone" android:hint= "@string/defaul_prop" > <requestfocus/&
Gt </EditText> <button android:id= "@+id/btn_dail" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_alignparentright= "true" android:layout_below= "@+id/et_number" Android:tex t= "@string/dail"/> </RelativeLayout>
Third, write Java code
3.1, Mainactivity.java
Package com.pb.dial;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import android.app.Activity;
Import android.content.Intent; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
OnCreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ID is found by ID, button bt_dail= (button) Findviewbyid (R.id.btn_dail);
Set the Click event Bt_dail.setonclicklistener for the key (new Myonclicklistener ()); //Click on the Listener event private class Myonclicklistener implements onclicklistener{/** * Click on the method called when the key is clicked * * @Overr IDE public void OnClick (View v) {//Remove content in input box//First find ID edittext et_number= (edittext) Mainactivity.thi
S.findviewbyid (R.id.et_number);
Content String Phonenumber=et_number.gettext () based on ID. toString (); intention, want to do something Intent intent=new Intent ();
Start dialing the phone intent.setaction (intent.action_call);
Set action content URI: Uniform Resource Identifier, URL type Uniform Resource Locator Intent.setdata (Uri.parse ("Tel:" +phonenumber));
Open a new Interface startactivity (intent); }
}
}
or
Package com.pb.dial;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
Import android.app.Activity;
Import android.content.Intent;
public class Mainactivity extends activity implements onclicklistener{private Button bt_dail;
Private EditText Et_number;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ID is found based on ID, the ID of the dialing key bt_dail= (button) Findviewbyid (R.id.btn_dail);
Take out the contents of the input box//first find the ID//according to the ID out content et_number= (edittext) MainActivity.this.findViewById (R.id.et_number);
Set Click event for key Bt_dail.setonclicklistener (this);
@Override public void OnClick (View v) {String phonenumber=et_number.gettext (). toString (). Trim (); Determine if the content is empty textutils is a tool class if (TextutIls.isempty (PhoneNumber)) {Toast.maketext (this, "phone number cannot be empty", Toast.length_long). Show ();
If it is a content class please change this to mainactivity.this return;
//Intent to do something Intent intent=new Intent ();
Start dialing the phone intent.setaction (intent.action_call);
Set action content URI: Uniform Resource Identifier, URL type Uniform Resource Locator Intent.setdata (Uri.parse ("Tel:" +phonenumber));
Open a new Interface startactivity (intent);
//Click on the Listener Event/* Private class Myonclicklistener implements onclicklistener{*//** * Click the method that is invoked when the key is clicked *//* @Override public void OnClick (View v) {//Take out the contents in the Input box//First find the ID edittext et_number= (edittext) Mainactiv
Ity.this.findViewById (R.id.et_number);
Content String Phonenumber=et_number.gettext () based on ID. toString ();
intention, want to do something Intent intent=new Intent ();
Start dialing the phone intent.setaction (intent.action_call);
Set action content URI: Uniform Resource Identifier, URL type Uniform Resource Locator Intent.setdata (Uri.parse ("Tel:" +phonenumber));
Open a new Interface startactivity (intent); }
}*/
}
3.2, add permissions
<!--add permissions-->
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
3.3, Operation
Let's share a piece of code ———— Android phone dialing and SMS transmitter
Android Phone dialing device
Because the application wants to use the phone service of the mobile phone, add the telephony permissions to the manifest file Androidmanifest.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" cn.itcast.action "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
....
<USES-SDK android:minsdkversion= "6"/>
<uses-permission android:name= "Android.permission.CALL_PHONE "/>
</manifest>
Interface layout:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent" >
<textview android:layout_width= "fill_parent" android:layout_height= "WRAP_"
Content "
android:text=" @string/inputmobile "/>
<edittext android:layout_width=" Fill_parent "Android : layout_height= "wrap_content" android:id= "@+id/mobile"/> <button android:layout_width=
"Wrap_ Content "android:layout_height=" wrap_content "
android:text=" "@string/button"
android:id= "@+id/button"/ >
</LinearLayout>
LinearLayout (linear layout), Absolutelayout (absolute layout), relativelayout (relative layout), tablelayout (table layout), Framelayout (frame layout)
Activity:
public class Dialeraction extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Button button = (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {public
void OnClick (View v) {
EditText edittext = ( EditText) Findviewbyid (r.id.mobile);
Intent Intent = new Intent (Intent.action_call, Uri.parse ("Tel:" + edittext.gettext ()));
DialerAction.this.startActivity (intent);}}
);
}
SMS Transmitter
Because the application to use the mobile phone SMS service, so in the manifest file Androidmanifest.xml to add SMS service permissions:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
Interface layout:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Fill_parent "Android: layout_height= "Fill_parent" > <textview android:layout_width= "fill_parent" android:layout_height= "WRAP_" Content "android:text=" @string/inputmobile "/> <edittext android:layout_width=" Fill_parent "Android:layout_ height= "Wrap_content" android:id= "@+id/mobile"/> <textview android:layout_width= "Fill_parent" android:layout _height= "Wrap_content" android:text= "@string/content"/> <edittext android:layout_width= "Fill_parent" Android : layout_height= "Wrap_content" android:minlines= "3" android:id= "@+id/content"/> <button, android:layout_width=
"Wrap_content" android:layout_height= "wrap_content" android:text= "@string/button" android:id= "@+id/button"/> </linearlayout>
Activity Main code:
String mobile = Mobileview.gettext (). toString ();
String content = Contentview.gettext (). toString ();
Smsmanager Smsmanager = Smsmanager.getdefault ();
Pendingintent sentintent = pendingintent.getbroadcast (smssender.this, 0, New Intent (), 0);
if (Content.length () >70) {//If the word number is more than 70, split into more than one text message sent
list<string> msgs = smsmanager.dividemessage (content);
for (String msg:msgs) {
smsmanager.sendtextmessage (mobile, NULL, MSG, sentintent, null);
The last two parameters for the SMS have been sent by the broadcast intent, the last parameter for the SMS message has received the message of the broadcast intent
}
}else{
smsmanager.sendtextmessage (mobile, NULL, Content, sentintent, null);
}
Toast.maketext (smssender.this, "SMS Send Complete", Toast.length_long). Show ();