gpt4 book ai didi

java - 安卓 3.1 : Refactoring Button Code and Permissions to MVP Architecture

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:09 24 4
gpt4 key购买 nike

我有一个简单的一键式应用程序。该应用程序显示一个调用电话的按钮。我无法清楚地将代码重构为 MVP 架构。有一个类似的 SO 问题 ( What's the best way to check for permissions at runtime using MVP architecture? )。但是,在这个问题中,答案不够明确,无法将其应用于我的问题。

权限请求在演示者的 View 中未被识别。我遇到的问题是在 startActivity(intent); 的 View 中。 Android Studio 用红色强调了 Intent 并告诉我“调用需要可能被用户拒绝的权限。代码应该明确检查权限”。

为什么这种重构是错误的,我该如何纠正它以遵守 MVP 架构?

public class MainActivity extends AppCompatActivity {

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

public void BtnSetEmergency_onclick(View view) {
String number = "1112223333";
final int REQUEST_PHONE_CALL = 1;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

Toast.makeText(MainActivity.this, "One Button App", LENGTH_SHORT).show();

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else{
startActivity(intent);
}

return;
}
startActivity(intent);
}
}

我重构为 MVP 架构的尝试

查看

public class MainActivity extends AppCompatActivity {

mainActivityPresenter mainActivityPresenterObject = new mainActivityPresenter();

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

}


public void BtnSetEmergency_onclick(View view) {
boolean phonePermissions = false;
String number = "1112223333";
final int REQUEST_PHONE_CALL = 1;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));


phonePermissions = mainActivityPresenterObject.checkPhonePermissions(view);

if(phonePermissions == true){
startActivity(intent);
}else if (phonePermissions == false){

}
}
}

演示者

public class mainActivityPresenter {

public boolean checkPhonePermissions(View view){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

Toast.makeText(MainActivity.this "One Button App", LENGTH_SHORT).show();

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
return false;
}
else
{
return true;
}
}
return false;
}
}

============================================= ============>

编辑:应用基于 Nilesh Rathod 答案的最终答案

============================================= ============>

查看

public class MainActivity extends AppCompatActivity {

mainActivityPresenter mainActivityPresenterObject = new mainActivityPresenter();

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


public void BtnSetEmergency_onclick(View view) {
boolean phonePermissions = false;
int number = 1112223333;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + Integer.toString(number)));

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}else {
mainActivityPresenterObject.checkPhonePermissions(view, MainActivity.this);
}
}
}

演示者

public class mainActivityPresenter {
final int REQUEST_PHONE_CALL = 1;

public void checkPhonePermissions(View view, MainActivity mainActivity){
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}

}

最佳答案

"Call requires permission which may be rejected by the user. The code should explicitly check for permissions"

它的行为符合预期。

调用startActivity(intent);

调用前需要检查用户是否授予调用权限

仅供引用

您可以使用 ActivityCompat.OnRequestPermissionsResultCall 处理权限对话框的结果

This interface is the contract for receiving the results for permission requests

试试这个

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}else {
// ask here for call permission
}

关于java - 安卓 3.1 : Refactoring Button Code and Permissions to MVP Architecture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745115/

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