android向我发送两次相同的消息

qvtsj1bj  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(143)

我正在为学校做一个项目,并尝试向用户手机发送短信。这个消息被发送了两次而不是一次。我该如何解决这个问题?
这是我的密码:

private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =1 ;
EditText textphone;
String message;
String phoneNo;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_r_e_g_i_s_t_e_r);
    textphone=findViewById(R.id.phone);
c = findViewById(R.id.create1);
    c.setOnClickListener(this);
 c.setEnabled(false);

    if(!checkPermission(Manifest.permission.SEND_SMS)){
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);
    }else{
        c.setEnabled(true);
    }

}
public void onClick(View v) {
    if (v == c) {
 sendmessage();}

现在方法是:

private void sendmessage() {

    if(checkPermission(Manifest.permission.SEND_SMS))
    {
        message="Hello "+ username2.getText().toString()+",welcome to Chatime. Hope you enjoy from our app.";
        phoneNo=textphone.getText().toString();
        if(phoneNo.length()==0){
            return;
        }
        SmsManager smsManager=SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo,null,message,null,null);
        Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_SHORT).show();
    }else {
        Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
    }
}

public boolean checkPermission(String permission){
    int check=ContextCompat.checkSelfPermission(this,permission);
    return (check==PackageManager.PERMISSION_GRANTED);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题