gpt4 book ai didi

Android 短信管理器不发送短信

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:37 26 4
gpt4 key购买 nike

Android 的新功能。我想在点击发送按钮后发送短信

  1. 首先我使用了短信管理器 api。
package com.example.smsproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;`enter code here`
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Page2Activity extends Activity {

Button button;
EditText textPhoneNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button1);
textPhoneNo = (EditText) findViewById(R.id.mobilenumber);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v){

//String phoneNo = textPhoneNo.getText().toString();
String phoneNo = "tel:xxxxxxxxxx";
String messageText = "SMS FROM ANDROID";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, messageText, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent Successfully!",
Toast.LENGTH_LONG).show();
}catch (Exception e){

Toast.makeText(getApplicationContext(),
"SMS failed, please try again later ! ",
Toast.LENGTH_LONG).show();
e.printStackTrace();

}

}

});

}

}
  1. 在 android_manifest.xml 上设置 send_sms 权限

我的错误为零,但短信未发送。如果你知道答案。

请告诉我,感谢阅读。

最佳答案

完成@Android Fanatic 的回答

如果文本太长,消息不会消失,您必须根据编码遵守最大长度。

可以找到更多信息here .

我更喜欢这种方法

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);

ArrayList<PendingIntent> sendList = new ArrayList<>();
sendList.add(sentPI);

ArrayList<PendingIntent> deliverList = new ArrayList<>();
deliverList.add(deliveredPI);

sms.sendMultipartTextMessage(phoneNumber, null, parts, sendList, deliverList);

关于Android 短信管理器不发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com