我一直试图通过我的应用程序发送短信,而无需打开deafult短信应用程序。 但我失败了。 这是我的主要代码
Snackbar snackbar = Snackbar
.make(recyclerView, "This will send a SMS!!!", Snackbar.LENGTH_INDEFINITE).setAction("Confirm", new View.OnClickListener() {
@Override
public void onClick(View view) {
String message;
if(type.equals("DELIVERED")){
message="Order from " + data.shopz.name + " will be delivered to room "+orders.get(clickedPosRecycler).custRoom
+ " in hostel "+orders.get(clickedPosRecycler).custHostel+" in 10 minutes.";
}else{
message = "Order placed at "+ data.shopz.name+" has been cancelled .Sorry for the inconvenience. An amount of Rs."
+ orders.get(clickedPosRecycler).amount+" has been credited back to your account.";
}
orders.remove(clickedPosRecycler);
orderAdapter.notifyItemRemoved(clickedPosRecycler);
try{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(thisActivity, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(thisActivity, 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 smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("0"+ mobile, null, message, sentPI, deliveredPI);}catch(Exception e){
Toast.makeText(thisActivity, "SMS failed",
Toast.LENGTH_LONG).show();
}
}
});
snackbar.show();
我添加了
import android.telephony.SmsManager;
并添加了权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cse.pec.foodapp">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />........
我已尝试过所有版本的手机号码格式
9876543210,+ 919876543210,09876543210(印度数字)
它总是给出 Genric Failure
到目前为止,我已经在Android 4.4.4以及6.0和5.1上进行了测试。它失败了。
答案 0 :(得分:0)
授予应用程序在手机中发送短信的权限。
转到“设置&gt;应用&gt;已安装的应用&gt;'your_app_name'&gt;应用 权限“并启用发送短信的权限。
答案 1 :(得分:0)
您可能正在使用双SIM卡,在我的情况下,我选择了用于发送消息的SIM卡...所以我在设置中将其更改了