我正在尝试输入消息并将其作为短信发送。短信无法提供。是否有输入手机号码的格式或代码有问题?
public class NewMessageActivity extends AppCompatActivity {
private String message, number;
private EditText mComposeMessage, mPhoneNumber;
private Button mSendButton, mCancelButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_message_layout);
mPhoneNumber = (EditText) findViewById(R.id.phone_number_et);
mComposeMessage = (EditText) findViewById(R.id.compose_et);
mSendButton = (Button) findViewById(R.id.send_button);
mCancelButton = (Button) findViewById(R.id.cancel_button);
mSendButton.setOnClickListener(mButtonClickListener);
mCancelButton.setOnClickListener(mButtonClickListener);
}
private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.send_button:
number = mPhoneNumber.getText().toString();
message = mComposeMessage.getText().toString();
if (number.length() > 0 && message.length() > 0){
Intent intent = new Intent(getApplicationContext(), SentActivity.class);
intent.putExtra("NUMBER", number);
intent.putExtra("MESSAGE", message);
startActivity(intent);
}
break;
case R.id.cancel_button:
break;
}
}
};
}
这是我调用IntentService的地方
public class SentActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
Bundle bundle = new Bundle();
final ArrayList<Message> messages = new ArrayList<>();
NetworkInfo info;
private static final int MESSAGE_LOADER = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.message_recycler_list);
mRecyclerView = (RecyclerView) findViewById(R.id.message_recycler_view);
mAdapter = new SentMessageAdapter(messages);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
mAdapter.notifyDataSetChanged();
Bundle extras = getIntent().getExtras();
String message = "";
String number = "";
if (extras != null){
message = extras.getString(String.valueOf("MESSAGE"));
number = extras.getString(String.valueOf("NUMBER"));
messages.add(new Message(number, message));
}
Intent serviceIntent = new Intent(getApplicationContext(), NotificationService.class);
serviceIntent.putExtra("MESSAGE", message);
serviceIntent.putExtra("NUMBER", number);
startService(serviceIntent);
}
这是IntentSevice
public class NotificationService extends IntentService {
public NotificationService() {
super(NotificationService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null){
String number = extras.getString(String.valueOf("NUMBER"));
String message = extras.getString(String.valueOf("MESSAGE"));
sendMessage(number, message);
}
}
private void sendMessage(String number, String message) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, new Intent("SMS_SENT"), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, pendingIntent, null);
}
}
这是我的清单文件
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
答案 0 :(得分:0)
试试这个
sendSMS(phoneNo, message);//message-just a value from EditText
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//—when the SMS has been sent—
registerReceiver(new BroadcastReceiver(){
@Override
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;
}
}
}, new IntentFilter(SENT));
//—when the SMS has been delivered—
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
请参阅此https://mrbrownsandroid.blogspot.in/2016/10/sms-messaging-android-tutorial.html