权限在运行时授予Action_Call意图

时间:2016-07-17 09:42:15

标签: android runtime-permissions

使用此代码将用户发送到拨号屏幕非常简单

    EditText firstNumber;
    Button btnAdd;
    String hash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main23);

        btnAdd = (Button)findViewById(R.id.button2);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {

                    EditText et = (EditText) findViewById(R.id.editText);


                    String text= et.getEditableText().toString();
                    hash = "#";


                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*221*" + text + "hash"));
                    startActivity(intent);
                } catch (Exception e) {
          }
        }
     });

    }

但是,当我希望用户通过按钮自动拨打电话时,按钮应自动拨打电话。我用 android.permission.CALL_PHONE 设置了它,我使用下面的代码 ....

EditText firstNumber;
EditText secondNumber;
Button   btnAdd;



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


    //assigning where the button5 to btnAdd
    btnAdd = (Button)findViewById(R.id.button5);

    // set up my setOnClickListener for button
    btnAdd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {

                //stores whats inside of the editText2 to et and et2
                EditText et = (EditText) findViewById(R.id.editText2);
                EditText et2 = (EditText) findViewById(R.id.editText5);

                //stores whats been sent to et and et2 variables to text and text2
                String text= et.getEditableText().toString();
                String text2 =et2.getEditableText().toString();


                // new intent to take user to dial screen and make the call automatically.
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                // crashes here "i get red squigglies here "
                startActivity(callIntent);

            } catch (Exception e) {

          }
        }
    });

}

我在 startActivity(callIntent); 下得到了一个波浪形状,说明“呼叫需要用户可能拒绝的许可。代码要明确检查以查看权限是否可用”。我如何让它在运行时运行?

1 个答案:

答案 0 :(得分:0)

您应该使用动态权限请求(运行时请求)

    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    EditText in_number;
    Intent c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__phone_call);

        in_number = (EditText) findViewById(R.id.in_number);


    }

    public void CALL(View v) {
        int toCall = Integer.parseInt(in_number.getText().toString());

        c = new Intent(Intent.ACTION_CALL);
        c.setData(Uri.parse("tel:"+toCall));
        startActivity(c);


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

            // Should we show an explanation?
 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
Toast.makeText(this,"FROM shouldShowRequestPermissionRationale",Toast.LENGTH_LONG).show();

            } else {

                // No explanation needed, we can request the permission.

 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.

     Toast.makeText(this,"FROM requestPermissions",Toast.LENGTH_LONG).show();
            }
        }


    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
                // If request is cancelled, the result arrays are empty.
  if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    Toast.makeText(this,"FROM starting ACT",Toast.LENGTH_LONG).show();


                } else {
                    Toast.makeText(this,"FROM problem",Toast.LENGTH_LONG).show();

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

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



}

... XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_editor_absoluteX="0dp"
    app:layout_editor_absoluteY="0dp"
    tools:context="com.jupiter.alienjo.intents.Activity_TakePicture">

    <RelativeLayout
        android:layout_width="344dp"
        android:layout_height="495dp"
        app:layout_editor_absoluteX="0dp"
        app:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="-185dp">

        <Button
            android:id="@+id/btn_call"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:onClick="CALL"
            android:text="call this Number"
            android:textSize="24sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/in_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/btn_call"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="71dp"
            android:ems="10"
            android:inputType="number" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

...还要将其添加到清单文件中以支持旧版本api

<uses-permission android:name="android.permission.CALL_PHONE" />