无法在 Android 中使用 SMSManager 发送短信

发布于 12-29 03:02 字数 1977 浏览 5 评论 0原文

在我的应用程序中,我不想使用默认消息发送器。为此,我遵循以下链接 在 Android 中是否可以向多个收件人发送短信代码?

  • 而且该代码也有效。但我从这里发送的消息 代码不会保存在手机发件箱和收件箱中。
  • 我在代码中使用这样的短信管理器

    SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null);

但它没有发送短信。请帮助我如何在 Android 中发送短信 - 我也尝试过关注 PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent( 已发送), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

它也不起作用。
SMSAPPActivity.java

编辑:

btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message = txtMessage.getText().toString();
                String[] PhnNoArray = new String[2];
                PhnNoArray[0] = "9999999999";
                PhnNoArray[1] = "8888888888";
                // StringTokenizer st = new StringTokenizer(phoneNo, ",");
                smsManager = SmsManager.getDefault();
                for (int i = 0; i < PhnNoArray.length; i++) {
                    smsManager = SmsManager.getDefault();
                        // this is the function that does all the magic
//                      sms.sendTextMessage(phoneNumber, null, msg, pi, null);
                    smsManager.sendTextMessage(PhnNoArray[i], null, message, null,
                            null);
                    Toast.makeText(getBaseContext(), "SMS sent : " + i,
                            Toast.LENGTH_SHORT).show();
                }
}
        });

请查看编辑并告诉我我做错了什么。tost 已显示,但使用此代码在其他手机上未收到短信

In my application I do not want to use the default message sender. For doing that I followed the following link In Android is it possible to send sms message to more than one recipient in code?

  • And that code worked too. But the messages I am sending from this
    code are not saved on the phones outbox and inbox.
  • I am using sms manager like this in my code

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);

But it is not sending sms.please help me with how can i send sms in android
- i have tried following too
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

It's also not working.
SMSAPPActivity.java

EDIT :

btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message = txtMessage.getText().toString();
                String[] PhnNoArray = new String[2];
                PhnNoArray[0] = "9999999999";
                PhnNoArray[1] = "8888888888";
                // StringTokenizer st = new StringTokenizer(phoneNo, ",");
                smsManager = SmsManager.getDefault();
                for (int i = 0; i < PhnNoArray.length; i++) {
                    smsManager = SmsManager.getDefault();
                        // this is the function that does all the magic
//                      sms.sendTextMessage(phoneNumber, null, msg, pi, null);
                    smsManager.sendTextMessage(PhnNoArray[i], null, message, null,
                            null);
                    Toast.makeText(getBaseContext(), "SMS sent : " + i,
                            Toast.LENGTH_SHORT).show();
                }
}
        });

Please see the edit and tell me what i have done wrong.tost is showing up but sms is not received on other phone by using this code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷乱花海2025-01-05 03:02:20

如果您使用双卡设备,那么您必须提及发件人号码,此时您不能传递 null。否则,SmsManager 将引发名为 SmsManager.RESULT_ERROR_GENERIC_FAILURE 的错误。

检查活动SIM卡数量的代码:

public static int getNumberOfActiveSim(Context context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
                List<SubscriptionInfo> subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoList();
                return subscriptionInfo != null ? subscriptionInfo.size() : 0;
            }
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if(telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT){
                return 1;
            }
            return 0;
        }

If you are using dual sim device then you must have to mention the sender number, You can't pass null at that time. Otherwise, SmsManager will throw an error called SmsManager.RESULT_ERROR_GENERIC_FAILURE.

Code to check numbers of active sims:

public static int getNumberOfActiveSim(Context context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
                List<SubscriptionInfo> subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoList();
                return subscriptionInfo != null ? subscriptionInfo.size() : 0;
            }
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if(telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT){
                return 1;
            }
            return 0;
        }
海的爱人是光2025-01-05 03:02:20

1) 在“已发送”而不是“发件箱”中添加邮件,因为“发件箱”包含要发送或处于发送状态的邮件。

2)当您发送消息时,将它们同时添加到“content://sms/sent uri”中。

是什么阻止您将它们存储在数据库中。以及您还尝试过什么。

使用下面的代码发送短信

 smsManager.sendTextMessage(number, null,desc, null, null);


通过使用 content://sms/sent URI,您可以将相同的文本消息插入消息数据库

1) add messages in Sent instead of Outbox, as Outbox contains messages which are suppose to send or in sending state.

2) when you send message add them at the same time in "content://sms/sent uri.

what is stopping u to store them in database. and what you tried yet.

use below code to sendSMS

 smsManager.sendTextMessage(number, null,desc, null, null);

and
by using content://sms/sent URI, you can insert the same text message into Message database

美胚控场2025-01-05 03:02:20

希望这可以帮助你。

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.*;


public class MainActivity extends Activity implements OnClickListener{


Button click;
EditText txt;
TextView txtvw;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

click = (Button)findViewById(R.id.button);
txt = (EditText)findViewById(R.id.editText);
txtvw = (TextView)findViewById(R.id.textView1);

click.setOnClickListener(this);
}

@Override
public void onClick(View v){


txt.setText("");
v = this.getCurrentFocus();

try{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage("8017891398",null,"Sent from Android",null,null);
}
catch(Exception e){
    txtvw.setText("Message not sent!");
}
if(v != null){
    InputMethodManager imm =   (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(),0);
  }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
   }

}

AndroidManifest.xml 中添加此行

<uses-permission android:name="android.permission.SEND_SMS" />

在此处输入图像描述

Hope this can help you.

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.*;


public class MainActivity extends Activity implements OnClickListener{


Button click;
EditText txt;
TextView txtvw;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

click = (Button)findViewById(R.id.button);
txt = (EditText)findViewById(R.id.editText);
txtvw = (TextView)findViewById(R.id.textView1);

click.setOnClickListener(this);
}

@Override
public void onClick(View v){


txt.setText("");
v = this.getCurrentFocus();

try{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage("8017891398",null,"Sent from Android",null,null);
}
catch(Exception e){
    txtvw.setText("Message not sent!");
}
if(v != null){
    InputMethodManager imm =   (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(),0);
  }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
   }

}

add this line in AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />

enter image description here

别忘他2025-01-05 03:02:20

只需使用 SmsManager 直接发送即可。唯一的问题是用户不会知道它。

just send it directly... using the SmsManager. Only problem is that is that the user won't know of it.

澜川若宁2025-01-05 03:02:20

就我而言,消息正文超过 160 个字符,我必须使用 sendMultipartTextMessage 代替 sendTextMessage

In my case, it was the fact that the message body exceeded 160 characters and I had to use sendMultipartTextMessage in lieu of sendTextMessage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文