Question

J'utilise le code suivant pour faire un appel dans Android, mais il me donne exception de sécurité aide s'il vous plaît.

 posted_by = "111-333-222-4";

 String uri = "tel:" + posted_by.trim() ;
 Intent intent = new Intent(Intent.ACTION_CALL);
 intent.setData(Uri.parse(uri));
 startActivity(intent);

autorisations

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

Exception

11-25 14:47:01.661: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
11-25 14:47:01.681: ERROR/AndroidRuntime(302): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:111-333-222-4 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43d32508 302:com.Finditnear/10026} (pid=302, uid=10026) requires android.permission.CALL_PHONE
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.os.Parcel.readException(Parcel.java:1218)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.os.Parcel.readException(Parcel.java:1206)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1214)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Activity.startActivityForResult(Activity.java:2749)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Activity.startActivity(Activity.java:2855)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at com.Finditnear.PostDetail$2$1$1$1.onClick(PostDetail.java:604)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.ListView.performItemClick(ListView.java:3285)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
Était-ce utile?

La solution 4

Tout est bien.

i autorisations d'appel vient de placer étiquette avant étiquette d'application dans le fichier manifeste

et maintenant tout fonctionne bien.

Autres conseils

Vous pouvez utiliser Intent.ACTION_DIAL au lieu de Intent.ACTION_CALL. Cela montre le composeur avec le numéro déjà entré, mais permet à l'utilisateur de décider de faire réellement l'appel ou non. ACTION_DIAL ne nécessite pas l'autorisation de CALL_PHONE.

Cette démo sera utile pour vous ...

Le bouton d'appel, cliquez sur:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);

Permission de Manifest:

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

option Plus élégante:

String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);

Utilisez le ACTION_DIAL d'action dans votre intention, de cette façon vous ne aurez pas besoin d'aucune autorisation. La raison pour laquelle vous avez besoin avec la permission ACTION_CALL est de faire un appel téléphonique sans aucune action de l'utilisateur.

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0987654321"));
startActivity(intent);

NOTE IMPORTANTE:

Si vous utilisez Intent.ACTION_CALL vous devez ajouter l'autorisation CALL_PHONE.

Son Okey que si vous ne voulez pas que votre application apparaisse dans le jeu de Google pour les tablettes qui ne prend pas de carte SIM ou ne possède pas GSM.

DANS VOTRE ACTIVITÉ:

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
            startActivity(callIntent);

MANIFESTE:

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

Donc, si ce n'est pas caractéristique essentielle à votre application, essayez de rester à l'écart d'ajouter l'autorisation de CALL_PHONE.

AUTRE SOLUTION:

est de montrer l'application de téléphone avec le numéro écrit sur l'écran, donc l'utilisateur ne doit cliquer sur le bouton d'appel:

            Intent dialIntent = new Intent(Intent.ACTION_DIAL);
            dialIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
            startActivity(dialIntent);

Aucune autorisation nécessaire à cet effet.

Juste simple oneliner sans autorisations supplémentaires nécessaires:

private void dialContactPhone(final String phoneNumber) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}

utiliser ce code complet

          Intent callIntent = new Intent(Intent.ACTION_DIAL);
          callIntent.setData(Uri.parse("tel:"+Uri.encode(PhoneNum.trim())));
          callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(callIntent);     

Demander la permission dans le manifeste

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

En appelant ce code

Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:99xxxxxxxx"));
try {
    startActivity(in);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(mcontext, "Could not find an activity to place the call.", Toast.LENGTH_SHORT).show();
}

Permissions:

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

Intention:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
startActivity(callIntent);

L'autorisation dans AndroidManifest.xml

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

Code complet:

private void onCallBtnClick(){
    if (Build.VERSION.SDK_INT < 23) {
        phoneCall();
    }else {

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

            phoneCall();
        }else {
            final String[] PERMISSIONS_STORAGE = {Manifest.permission.CALL_PHONE};
            //Asking request Permissions
            ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, 9);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    boolean permissionGranted = false;
    switch(requestCode){
        case 9:
            permissionGranted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
            break;
    }
    if(permissionGranted){
        phoneCall();
    }else {
        Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
    }
}

private void phoneCall(){
    if (ActivityCompat.checkSelfPermission(mActivity,
            Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:12345678900"));
        mActivity.startActivity(callIntent);
    }else{
        Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
    }
}

Vous pouvez utiliser ce ainsi:

String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");

Pour faire une activité d'appel à l'aide des intentions, vous devez demander les autorisations appropriées.

Pour que vous incluez utilise des autorisations dans le fichier AndroidManifest.xml.

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

Ensuite, inclure le code suivant dans votre activité

if (ActivityCompat.checkSelfPermission(mActivity,
        Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    //Creating intents for making a call
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:123456789"));
    mActivity.startActivity(callIntent);

}else{
    Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
}

Pour éviter cela - on peut utiliser l'interface graphique pour la saisie des autorisations. Eclipse prendre soin de l'endroit où insérer la balise d'autorisation et plus souvent pas est correcte

Dans Android pour certaines fonctionnalités dont vous avez besoin pour ajouter l'autorisation au fichier Manifest.

  1. Aller aux projets AndroidManifest.xml
  2. Cliquez sur l'onglet Autorisations
  3. Cliquez sur Ajouter
  4. Sélectionnez Utilise autorisation
  5. Voir l'instantané ci-dessous entrer image description ici

6.Save le fichier manifeste et puis exécutez votre projet. Votre projet devrait maintenant fonctionner comme prévu.

11-25 14:47:01.681: ERROR/AndroidRuntime(302): blah blah...requires android.permission.CALL_PHONE

^ La réponse se trouve dans la sortie d'exception " requires android.permission.CALL_PHONE ":)

    final public void Call(View view){

    try {

        EditText editt=(EditText)findViewById(R.id.ed1);
        String str=editt.getText().toString();
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+str));
        startActivity(intent);
    }
    catch (android.content.ActivityNotFoundException e){

        Toast.makeText(getApplicationContext(),"App failed",Toast.LENGTH_LONG).show();
    }
 if(ContextCompat.checkSelfPermission(
        mContext,android.Manifest.permission.CALL_PHONE) != 
              PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions((Activity) mContext, new 
                  String[]{android.Manifest.permission.CALL_PHONE}, 0);
                } else {
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Number")));
                }

Pour appel de dialer (Aucune autorisation nécessaire):

   fun callFromDailer(mContext: Context, number: String) {
        try {
            val callIntent = Intent(Intent.ACTION_DIAL)
            callIntent.data = Uri.parse("tel:$number")
            mContext.startActivity(callIntent)
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
        }
    }

Pour appel direct à partir de l'application (Permission nécessaire):

  fun callDirect(mContext: Context, number: String) {
        try {
            val callIntent = Intent(Intent.ACTION_CALL)
            callIntent.data = Uri.parse("tel:$number")
            mContext.startActivity(callIntent)
        } catch (e: SecurityException) {
            Toast.makeText(mContext, "Need call permission", Toast.LENGTH_LONG).show()
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
        }
    }

Autorisation:

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top