Отправка SMS с KitKat и выше: -
Добавьте это разрешение в ваш AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
Вам также нужно будет реализовать разрешение времени выполнения для Marshmallow и версии выше.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.defaultmessanginggit">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ConversationListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ComposeSMSActivity"
android:label="@string/title_activity_compose_sms" >
</activity>
</application>
</manifest>
Код, который будет приведен ниже: -
activity_conversation_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<Button
android:id="@+id/btn_send_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message" />
</LinearLayout>
ConversationListActivity.java
public class ConversationListActivity extends FragmentActivity {
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private int PERMISSIONS_REQUEST_RECEIVE_SMS = 130;
private Button btn_send_sms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_list);
btn_send_sms = (Button) findViewById(R.id.btn_send_msg);
btn_send_sms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hasSendSMSPermission = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hasSendSMSPermission = checkSelfPermission(Manifest.permission.SEND_SMS);
if (hasSendSMSPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS},
PERMISSIONS_REQUEST_RECEIVE_SMS);
} else if (hasSendSMSPermission == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(ConversationListActivity.this, ComposeSMSActivity.class);
startActivity(intent);
}
}else{
Intent intent = new Intent(ConversationListActivity.this, ComposeSMSActivity.class);
startActivity(intent);
}
}
});
}
}
Это код для размещения смс и отправки SMS: -
activity_compose_sms.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />
</LinearLayout>
fragment_compose_sms.xml
<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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/compose_to"
android:id="@+id/textView"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/composeEditTextTo" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/compose_message"
android:id="@+id/textView2"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/composeEditTextMessage"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/compose_cancel"
android:id="@+id/composeButtonCancel" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/compose_send"
android:id="@+id/composeButtonSend" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:id="@+id/composeNotDefault"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/compose_not_default"
android:id="@id/composeNotDefault" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/compose_set_default"
android:id="@+id/composeButtonSetDefault" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ComposeSMSActivity.java
public class ComposeSMSActivity extends Activity {
Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compose_sms);
mActivity = this;
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_compose_sms, container, false);
rootView.findViewById(R.id.composeButtonCancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpTo(getActivity(), new Intent(getActivity(), ConversationListActivity.class));
}
});
rootView.findViewById(R.id.composeButtonSend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String recipient = ((TextView) rootView.findViewById(R.id.composeEditTextTo)).getText().toString();
String message = ((TextView) rootView.findViewById(R.id.composeEditTextMessage)).getText().toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(recipient, "ME", message, null, null);
}
});
return rootView;
}
}
}
Вот и все.