2016-07-17 140 views
0

與ACTION_CALL意圖授予發送用戶撥打屏幕相當直截了當使用此代碼權限在運行時

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);指出「調用需要權限可能會被用戶拒絕。代碼來顯式檢查是否有權限可用」。我如何讓它在運行時運行?

+0

請參閱[在運行時請求權限](https://developer.android.com/training/permissions/requesting.html) – reVerse

+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" />