2016-07-17 18 views
0

einen Benutzer gesendet Bildschirm zu wählen ist ziemlich geradlinig mit diesem CodeBerechtigungen Erteilen mit Action_Call Absicht zur Laufzeit

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) { 
      } 
     } 
    }); 

    } 

Jedoch, wenn ich ein Benutzer möchte einen Anruf automatisch an, um von einer Schaltfläche klicken Sie auf die Schaltfläche sollte automatisch make ein Anruf. Ich stelle es haben sich mit android.permission.CALL_PHONE, verwende ich diese Code unten ....

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) { 

      } 
     } 
    }); 

} 

ich einen verschnörkelten unter startActivity (callIntent) erhalten; besagt, dass "Anruf erfordert eine Berechtigung, die vom Benutzer abgelehnt werden kann. Code, um explizit zu überprüfen, ob die Berechtigung verfügbar ist". Wie kann ich es zur Laufzeit ausführen?

+0

Siehe [Anfordern von Berechtigungen zur Laufzeit] (https://developer.android.com/training/permissions/requesting.html) – reVerse

+0

ja, aber ich verstehe immer noch nicht. bitte hilfe –

Antwort

0

sollten Sie verwenden dynamische Berechtigungsanfrage (Laufzeit Anfrage)

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> 

... Auch dies Ihre Manifest-Datei in den alten Versionen api unterstützen

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