Android 3.1:重构按钮代码和MVP架构权限
Android 3.1: Refactoring Button Code and Permissions to MVP Architecture
我有一个简单的一键式应用程序。该应用程序显示一个按钮,用于拨打 phone 电话。我无法清楚地将代码重构为 MVP 架构。有一个类似的 SO 问题 ()。但是,在这个问题中,答案不够明确,无法将其应用于我的问题。
在演示者的视图中无法识别权限请求。我遇到的问题是在 startActivity(intent); 的视图中。 Android Studio 在红色下划线并告诉我 "Call requires permission which may be rejected by the user. The code should explicitly check for permissions".
为什么这个重构是错误的,我该如何纠正它以遵循 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);
拨打电话前需要检查用户是否授权通话
FYI
您可以使用 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
}
我有一个简单的一键式应用程序。该应用程序显示一个按钮,用于拨打 phone 电话。我无法清楚地将代码重构为 MVP 架构。有一个类似的 SO 问题 (
在演示者的视图中无法识别权限请求。我遇到的问题是在 startActivity(intent); 的视图中。 Android Studio 在红色下划线并告诉我 "Call requires permission which may be rejected by the user. The code should explicitly check for permissions".
为什么这个重构是错误的,我该如何纠正它以遵循 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);
FYI
您可以使用 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
}