android.permission.CALL_PHONE

问题描述 投票:0回答:1

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

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;
    }}}
android android-studio permissions call
1个回答
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
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.