为什么我的按钮只工作一次?- Android工作室

qyuhtwio  于 2023-02-17  发布在  Android
关注(0)|答案(1)|浏览(188)

我正尝试TP发送短信点击按钮BT这个按钮是工作juste一次.我不知道我怎么能让它这样的按钮可以按下无限的时间.你能请帮我解决这个问题.这是我的代码:

Button sendBtn;

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



    sendBtn = (Button) findViewById(R.id.btnSendSMS);
    sendBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            sendSMSMessage();
        }
    });

}
protected void sendSMSMessage() {

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    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("+212xxxxxx", null, "Je suis en danger, voici ma localisation : https://goo.gl/maps/xamxKW62p34wWpBU8", null, null);
                Toast.makeText(getApplicationContext(),
                        "SMS sent.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                return;
            }
        }
    }

}
cgh8pdjw

cgh8pdjw1#

sendSMSMessage中缺少else分支,因此如果已授予SEND_SMS权限,则不会发送SMS。如果需要显示权限理由,则也不会执行任何操作,这会导致体验中断。
总的来说,我建议仔细阅读关于请求权限的文档。它解释了您使用onRequestPermissionsResult的方法以及新推荐的使用ActivityResultContracts的方法。
在不重写合约代码的情况下,我将如下修复它:

protected void sendSMSMessage() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
            // fixme: show explanation 
            // before requesting the permission again
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    } else {
        sendSmsImpl();
    }
}
@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) {
                sendSmsImpl();
            } else {
                // fixme: explain that it can't send SMS without the permission
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                return;
            }
            // !!! NOTE: you still need break inside switch/case
            // even with curly braces
            break;
        }
    }
}

private void sendSmsImpl() {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("+212xxxxxx", null, "Je suis en danger, voici ma localisation : https://goo.gl/maps/xamxKW62p34wWpBU8", null, null);
    //todo: use sentIntent argument of sendTextMessage to detect success/error
    Toast.makeText(getApplicationContext(),
        "SMS sent.", Toast.LENGTH_LONG).show();
}

相关问题