gpt4 book ai didi

java - 从 SMSPortalFragment 发送短信不起作用

转载 作者:行者123 更新时间:2023-12-02 11:54:50 25 4
gpt4 key购买 nike

我一直在尝试使用 send_sms api 发送测试消息。 我的 SMSPortalFragment.java 代码没有显示错误。 Logcat 中也没有显示其他错误。

  1. 代码在 Galaxy S8 中构建并安装。
  2. 应用程序运行起来很有趣。
  3. 转至 fragment_smsportal.xml 工作正常。
  4. 我可以输入我的姓名和消息。
  5. 单击按钮时,它会在运行时询问我的PERMISSION
  6. 即使我允许许可,也不会发送任何消息。

java代码:

private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;

Button sendButon;
EditText fullName;
EditText textMessage;
String phoneNo = "***********";
String name;
String message;
String sendMessage;

public SMSPortalFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View smsportal = inflater.inflate(R.layout.fragment_smsportal, container, false);

fullName = (EditText) smsportal.findViewById(R.id.fullName);
textMessage = (EditText) smsportal.findViewById(R.id.textMessage);
sendButon = (Button) smsportal.findViewById(R.id.sendButon);

sendButon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMSMessage();
}
});

return smsportal;
}

private void sendSMSMessage() {
name = fullName.getText().toString();
message = textMessage.getText().toString();
sendMessage = name + " " + message;


if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sendMessage, null, null);
Toast.makeText(getActivity(),"SMS sent.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(),"SMS failed, please try again.", Toast.LENGTH_LONG).show();
}
}
}

}

谁能告诉我我做错了什么?

PS:出于安全考虑,我从此代码中删除了我的电话号码。

最佳答案

Do not write your send logic in onRequestPermissionsResult()and

试试这个...

Manifest permission

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

and here is the snippet

   try {
Bundle msgDataBundle = intent.getExtras();
String mobileNumber = msgDataBundle.getString("MobileNumber");
SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(mobileNumber,null,"sam",null,null);
smsManager.sendTextMessage(mobileNumber, null, "Test Sample Message Text", null, null);
Toast.makeText(context, "SMS Sent!", Toast.LENGTH_LONG).show();
Log.d("---", "Sent");

} catch (Exception e) {
Toast.makeText(context, "SMS faild, please try again.", Toast.LENGTH_LONG).show();
Log.d("---", "Fail");
e.printStackTrace();
}

关于java - 从 SMSPortalFragment 发送短信不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672004/

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