This is mainly to realize the mobile phone communication, which mainly realizes the function of making calls and sending short messages.
The dialing interface is as follows (due to the compatibility problem of the emulator screen, the interface cannot be displayed normally, but it can be displayed normally on the mobile phone):
When each number is clicked, the imagebutton will change the image, as follows:
To achieve this function, the main thing is to monitor the imagebutton. The code is as follows:
imageButton1.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { //click if(event.getAction() == MotionEvent.ACTION_DOWN){ //reset the background image when pressed ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.d1)); }else if(event.getAction() == MotionEvent.ACTION_UP){ //松开 //Modify to the normal picture when lifted ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.d1)); } return false; } });
The code to implement the dialing function is as follows, and ACTION_CALL needs to be called:
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 }
xml for dial function:
<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:background="#80bbe7" 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="wrap_content" android:layout_height="match_parent" android:layout_weight="0.70" > <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:src="@drawable/c1" android:onClick="digital_click" android:background="#80bbe7" android:tag="1" /> <ImageButton android:id="@+id/ImageButton02" android:layout_width="100dp" android:layout_height="70dp" android:src="@drawable/c2" android:onClick="digital_click" android:background="#80bbe7" android:tag="2"/> <ImageButton android:id="@+id/ImageButton03" android:layout_width="100dp" android:layout_height="70dp" android:src="@drawable/c3" android:onClick="digital_click" android:background="#80bbe7" android:tag="3" /> </TableRow> <TableRow android:id="@+id/TableRow03" android:layout_width="110dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton04" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/c4" android:background="#80bbe7" android:tag="4" /> <ImageButton android:id="@+id/ImageButton05" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/c5" android:background="#80bbe7" android:tag="5" /> <ImageButton android:id="@+id/ImageButton06" android:layout_width="100dp" android:layout_height="70dp" android:onClick="digital_click" android:src="@drawable/c6" android:background="#80bbe7" android:tag="6"/> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="110dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton07" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c7" android:tag="7"/> <ImageButton android:id="@+id/ImageButton08" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c8" android:tag="8" /> <ImageButton android:id="@+id/ImageButton09" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c9" android:tag="9" /> </TableRow> <TableRow android:id="@+id/TableRow01" android:layout_width="110dp" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ImageButton10" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c10" android:tag="*"/> <ImageButton android:id="@+id/ImageButton11" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c0" android:tag="0"/> <ImageButton android:id="@+id/ImageButton12" android:layout_width="100dp" android:layout_height="70dp" android:background="#80bbe7" android:onClick="digital_click" android:src="@drawable/c11" android:tag="#" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="match_parent" 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:background="@drawable/b1" /> <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/b2" /> <ImageButton android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="delete" android:background="@drawable/b3" /> </LinearLayout> </LinearLayout>
It should be noted that the corresponding permissions must be set:
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.VIBRATE" />
SMS function (the renderings are as follows):
The main code to implement this function is:
public void send(View view){ String telMsg=MainActivity.this.tel.getText().toString(); String contentMsg=MainActivity.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 MainActivity.this.startActivity(it); }
layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" 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" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Recipient:" /> <EditText android:id="@+id/tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" > <requestFocus android:layout_width="wrap_content" /> </EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容:" /> <EditText android:id="@+id/content" android:layout_width="260px" android:layout_height="wrap_content" android:ems="10" android:lines="6" android:background="@layout/edit_bg" /> </TableRow> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="send" android:text="Send SMS" /> </TableLayout> </LinearLayout>
Here I use edit_bg.xml to modify the content box:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#EFEFEF"/> <corners android:radius="3dip" /> <stroke android:width="0.5px" android:color="#505050"/> </shape> </item> </layer-list>
Correspondingly, it should also be noted that the corresponding permissions must be set:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />