Android calls the method to send text messages
Function: Call the SMS function
1. Permissions
<uses-permission android:name=".SEND_SMS"/>
2. Specific implementation
Uri smstoUri = ("smsto:"); Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri); ("address","telephone number"); // If there is no phone number, it is the default, that is, it is empty when displayed.("sms_body","SMS content"); // Set the content sent("-dir/mms-sms"); startActivity(intent);
Activity code:
public class MainActivity extends Activity { private EditText phone ,message; private Button sendbtn; protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); phone = (EditText) findViewById(); message = (EditText) findViewById(); sendbtn = (Button) findViewById(); //Click to send a text message (new OnClickListener() { public void onClick(View v) { String p = ().toString(); String m = ().toString(); Uri smstoUri = ("smsto:"); // Resolve the address Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri); ("address",p); // If there is no phone number, it is the default, that is, it is empty when displayed. ("sms_body",m); // Set the content sent ("-dir/mms-sms"); startActivity(intent); } }); } }
Configuration file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="" android:label="@string/app_name" > <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> </application> <!-- Send SMS permission --> <uses-permission android:name=".SEND_SMS" /> </manifest>
Layout diagram:
<RelativeLayout xmlns:andro xmlns:tools="/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: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> <Button android: style="?android:attr/buttonStyleSmall" android:layout_width="150dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="28dp" android:text="Send" /> <EditText android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/sendbtn" android:layout_alignLeft="@+id/phone" android:layout_marginBottom="48dp" android:ems="10" /> </RelativeLayout>
The above is how Android calls SMS. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!