Today, we have learned about Android communication. We can write programs to call the built-in API of the system to easily implement some of the system's own functions. Let's talk about the implementation process.
The first is the design of the home page main_activity:
The functions of the home page mainly include number buttons, add contacts buttons, dial buttons, send SMS buttons, and send email buttons. The digital buttons are mainly realized by adding a background image to the picture button. Each digital button is bound to a number, such as 0, which is bound by the android:tag="0" statement. When the number 0 is obtained, the statement text=btnDigital.getTag is used. ().toString(); to get the number.
Then I will talk about the layout. Through this design, I have a better understanding of the layout. The first two days I used the direct drag button to write the layout, and then I knew a little bit about it. Now at least I can understand how to use it, especially Get familiar with table layout.
As shown in the figure above, the layout of the page mainly uses the layout of the table to realize the typesetting of numbers, and then the table is also divided into rows by TableRow. The implementation code is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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/editText1" android:layout_width="match_parent" android:layout_height="82dp" android:ems="10" android:background="#ffffffff" android:textSize="40sp" android:textStyle="bold" android:textColor="#ff333333" > <requestFocus /> </EditText> <TableLayout android:layout_width="300dp" android:layout_height="wrap_content" > <TableRow android:id="@+id/TableRow04" android:layout_width="110dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton01" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n1" android:tag="1" /> <ImageButton android:id="@+id/ImageButton02" android:layout_width="100dp" android:layout_height="70dp" android:src="@drawable/n2" android:onClick="digital_click" android:tag="2"/> <ImageButton android:id="@+id/ImageButton03" android:layout_width="100dp" android:layout_height="70dp" android:src="@drawable/n3" android:onClick="digital_click" android:tag="3" /> </TableRow> <TableRow android:id="@+id/TableRow03" android:layout_width="100dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton4" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n4" android:tag="4" /> <ImageButton android:id="@+id/ImageButton5" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n5" android:tag="5" /> <ImageButton android:id="@+id/ImageButton06" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n6" android:tag="6"/> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="100dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton07" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n7" android:tag="7"/> <ImageButton android:id="@+id/ImageButton08" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n8" android:tag="8" /> <ImageButton android:id="@+id/ImageButton09" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n9" android:tag="9" /> </TableRow> <TableRow android:id="@+id/TableRow01" android:layout_width="100dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton10" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n10" android:tag="*"/> <ImageButton android:id="@+id/ImageButton11" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n11" android:tag="0"/> <ImageButton android:id="@+id/ImageButton12" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/n12" android:tag="#" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="300dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.23" android:onClick="newPeople" android:background="@drawable/people" /> <ImageButton android:id="@+id/dialer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.17" android:onClick="dial" android:background="@drawable/callpeople" /> <ImageButton android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="delete" android:background="@drawable/delete" /> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/msg" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/msg" /> <ImageButton android:id="@+id/email" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="200dp" android:onClick="sendEmail" android:src="@drawable/email" /> </LinearLayout> </LinearLayout>
The implementation steps of the main functions are as follows:
1. When pressing the button, call the function to add the number:
//The action to be performed after the key is pressed public void digital_click(View view){ ImageButton btnDigital=(ImageButton) view; String text=btnDigital.getTag().toString(); currentNum.append(text);//currentNum is StringBuffer to store numbers display();//Call the function to display the number } //The function that displays the number in the display column public void display(){ textResult.setText(currentNum.toString()); }
2. When pressing the dial button, call the system API to dial:
// call the function to call the function public void dial(View view) { EditText text=(EditText)findViewById(R.id.editText1); String number=text.getText().toString(); Intent intent = new Intent(); intent.setAction(intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent);//The method will automatically add a category to the Intent: android.intent.category.DEFAULT }
tip: When calling the dialing function of the system, you need to add the dialing permission. We need to add the following statement in the Androidmanifest:
<uses-permission android:name="android.permission.CALL_PHONE"/>
3. Add contacts:
// create a new contact public void newPeople(View view){ //interface Intent it = new Intent(Intent.ACTION_INSERT,Uri.withAppendedPath(Uri.parse("content://com.android.contacts"),"contacts" )); it.setType("vnd.android.cursor.dir/person"); String number = textResult.getText().toString(); it.putExtra(android.provider.ContactsContract.Intents.Insert.SECONDARY_PHONE,number); startActivity(it); }
4. The function of deleting the number and digit delete key:
// function of delete key public void delete(View view){ if(currentNum.length()>=1){ currentNum.delete(currentNum.length()-1, currentNum.length()); } if(currentNum.length()==0){ Toast toast=Toast.makeText(this,"Please enter the number",100); toast.show(); display(); } textResult.setText(currentNum); }
5. Realization of page jump for SMS and email:
msg=(ImageButton)findViewById(R.id.msg); msg.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent it = new Intent(); it.setClass(MainActivity.this, msg.class); MainActivity.this.startActivity(it); } }); email=(ImageButton)findViewById(R.id.email); email.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent it = new Intent(); it.setClass(MainActivity.this, email.class); MainActivity.this.startActivity(it); } });
6. Design SMS pages and emails;
7. The realization of the function of SMS:
public class msg extends Activity { private EditText tel=null; private EditText content=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.msg); this.tel=(EditText)super.findViewById(R.id.tel); this.content=(EditText)super.findViewById(R.id.content); } public void send(View view){ String telMsg=msg.this.tel.getText().toString(); String contentMsg=msg.this.content.getText().toString(); Uri uri=Uri.parse("smsto:"+telMsg);//Receiver's mobile phone Intent it=new Intent(); it.setAction(Intent.ACTION_SENDTO);//Specify action, I want to send text message it.putExtra("sms_body", contentMsg);//Set the information content it.setType("vnd.android-dir/mms-sms");//Set MIME type it.setData(uri);//Set data, where to go msg.this.startActivity(it); } }
The final result:
when we click send, the function is called, the system will jump to your texting page, and the content will be written in the text message.
8. The function of sending emails is realized:
public class email extends Activity { private EditText rec=null; private EditText subject=null; private EditText emailText=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.email); this.rec=(EditText)super.findViewById(R.id.receive); this.subject=(EditText)super.findViewById(R.id.subject); this.emailText=(EditText)super.findViewById(R.id.emailText); } public void sendEmail(View view){ String recMsg=email.this.rec.getText().toString(); String contentMsg=email.this.emailText.getText().toString(); String subjectMsg=email.this.subject.getText().toString(); Intent emailIntent = new Intent(Intent.ACTION_SEND) ; emailIntent.setType("plain/text") ; // set type String address[] = new String[] {recMsg} ; emailIntent.putExtra(Intent.EXTRA_EMAIL, address) ;//Email address emailIntent.putExtra(Intent.EXTRA_SUBJECT, subjectMsg) ; emailIntent.putExtra(Intent.EXTRA_TEXT, contentMsg) ; startActivity(emailIntent); // execute jump }}
When entering the relevant content, click Send, and open it with the mobile phone's mail app, and there will be relevant content.
The above is all I have done today. I have learned how to call the API of the system to realize the communication function. As long as I know the relevant interfaces, it is relatively simple to implement the relevant functions.