0

我有一個自定義按鈕類,我已經請求CALL_PHONE權限按鈕click.Everything是好的,但我沒有得到onRequestPermissionResult()的用戶結果。在這裏下面我張貼我的自定義按鈕類。Android M Permissions:onRequestPermissionsResult()沒有在自定義按鈕類中調用

public class CustomCallExecutiveButton extends Button implements View.OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback { 

Context mContext; 
private Button mCallButton; 
private int REQUEST_CALL = 1; 

public CustomCallExecutiveButton(Context context) { 

    super(context); 
    mContext = context; 
    init(); 
} 

public CustomCallExecutiveButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mContext = context; 
    init(); 
} 

public CustomCallExecutiveButton(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    mContext = context; 
    init(); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public CustomCallExecutiveButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    mContext = context; 
    init(); 
} 


private void init() { 
    setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    CheckCallPermission(); 


} 
public void CheckCallPermission() { 
    // Check if we have write permission 
    int permission = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE); 
    if (permission != PackageManager.PERMISSION_GRANTED) { 
     requestCameraPermission(); 
    } else { 
     intentToCall(); 
    } 
} 

public void intentToCall() { 
    Intent callIntent = new Intent(Intent.ACTION_CALL); 
    callIntent.setData(Uri.parse("tel:" + "9069118117")); 
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    mContext.startActivity(callIntent); 
} 

public void requestCameraPermission() { 

    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext,Manifest.permission.CALL_PHONE)) { 
     openCallPermissionDailog(); 

    } else { 
     openCallPermissionDailog(); 
    } 
} 

public void openCallPermissionDailog() { 

    //Requesting camera and storage permission dailog 
    android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(mContext); 
    alert.setTitle("Permission"); 
    alert.setMessage("Phone State Permission Needed To Make Call."); 
    alert.setPositiveButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 
    alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //when user clicks on yes onRequestPermissionResult tells whether user granted permission or not. 
      ActivityCompat.requestPermissions((Activity) mContext, 
        new String[]{Manifest.permission.CALL_PHONE}, 
        REQUEST_CALL); 
     } 
    }); 

    alert.show(); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

    if (requestCode == REQUEST_CALL) { 

     if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      // Camera permission has been granted, preview can be displayed 
      intentToCall(); 
     } 


    } 

} 
} 

在此先感謝鄉親:)

回答

1

我沒有收到在onRequestPermissionResult()的用戶結果

是的,你是。它會去mContext所代表的任何活動的onRequestPermissionResult()

恕我直言,你在做什麼是違反分離的擔憂。一些更像控制器的東西,比如活動或片段,負責檢查和請求權限,而不是視圖。

這就是說,如果您希望保留更多現有代碼,歡迎您將活動轉交給視圖的onRequestPermissionResult()結果。

+0

這意味着我不能在我的自定義類中得到結果。我需要在所有使用我的自定義類的活動中實現onRequestPermissionResult。 – Arora