Ошибка "общего сбоя" при отправке SMS
Я пытаюсь отправить сообщение с одного устройства на другое с помощью диспетчера SMS.
Код успешно компилируется, но приложение может отправлять сообщения на номер 121 только при попытке ввода с другим номером, я получаю общую ошибку сбоя.
Что я делаю неправильно в следующем коде.
Android Manifest.xml
< ? xml version = "1.0"
encoding = "utf-8" ? >
< manifest xmlns : android = "http://schemas.android.com/apk/res/android"
package = "com.example.admin.sms" >
< uses - permission android: name = "android.permission.SEND_SMS" / >
< uses - permission android: name = "android.permission.RECEIVE_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 = ".MainActivity" >
< intent - filter >
< action android: name = "android.intent.action.MAIN" / >
< category android: name = "android.intent.category.LAUNCHER" / >
< /intent-filter> < /activity> < /application>
< /manifest>
Файл main.java выглядит следующим образом.
package com.example.admin.sms;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.*;
import android.telephony.SmsManager;
import android.view.View;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
public class MainActivity extends Activity {
EditText phoneNumber,message;
BroadcastReceiver sentReceiver,deliveredReceiver;
String SENT="SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PendingIntent sentPendIntent = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
final PendingIntent delivered_pendintent = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
sentReceiver = new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio OFF", Toast.LENGTH_SHORT).show();
break;
}
}
};
deliveredReceiver = new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS successfully delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Failure-SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(sentReceiver, new IntentFilter(SENT));
registerReceiver(deliveredReceiver, new IntentFilter(DELIVERED));
Button sendBtn = (Button) this.findViewById(R.id.send_button);
sendBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
phoneNumber = (EditText) findViewById(R.id.recvr_no);
message = (EditText) findViewById(R.id.txt_msg);
if (phoneNumber.getText().toString().trim().length() > 0 && message.getText().toString().trim().length() > 0) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber.getText().toString(), null, message.getText().toString(), sentPendIntent, delivered_pendintent);
} else {
Toast.makeText(MainActivity.this, "Either phone nmber or text is missing", Toast.LENGTH_SHORT).show();
}
}
});
Button cancelBtn = (Button) this.findViewById(R.id.cancel_button);
cancelBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
phoneNumber.setText("");
message.setText("");
}
});
}
}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message sending form"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/recvr_no"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/txt_msg"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:onClick="onClick"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:text="Send SMS" />
<Button
android:id="@+id/cancel_button"
android:text="Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/send_button"
android:layout_marginLeft="15dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"/>
</RelativeLayout>
</LinearLayout>