android.permission.CALL_PHONE

时间:2018-12-31 03:14:53

标签: android android-studio permissions call

我有一个通话方法。一切正常。但是我想在按下按钮时访问它,应用程序继续请求调用权限。我应该如何继续实现此功能?

public class Menu_A extends AppCompatActivity implements OnClickListener {

private InterstitialAd mInterstitialAd;

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

    Button one = (Button) findViewById(R.id.btnActTwo);
    one.setOnClickListener(this);
    Button two = (Button) findViewById(R.id.btnAftor);
    two.setOnClickListener(this);


    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            if(mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }}});}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnActTwo:
            Intent intent = new Intent(this, eda.class);
            startActivity(intent);
            break;

        case R.id.btnAftor:

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:8********"));


            if (ActivityCompat.checkSelfPermission(Menu_A.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
            {return;}
            startActivity(callIntent);
            break;
    }}}

1 个答案:

答案 0 :(得分:1)

尝试一下:

 case R.id.btnAftor:

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:8********"));


            if (ActivityCompat.checkSelfPermission(Menu_A.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
            {
                   ActivityCompat.requestPermissions(Menu_A.this, new String[]{Manifest.permission.CALL_PHONE}, 1);

      }
            startActivity(callIntent);
            break;

然后在您的Menu_A类中检查权限

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

        case 1: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                call_action();
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
 }