当我选择打电话时,应用程序崩溃了

时间:2014-03-20 10:14:12

标签: android

嗨我必须在我的应用程序中实现一些调用功能,但是当我选择一个号码进行呼叫时,我的应用程序突然崩溃。

我的代码是:

public void show_alert(String phon) {

            ArrayList<String> phone = new ArrayList<String>();

            if (!phon.equalsIgnoreCase("")) {
                phone.add(phon);
            }

            final CharSequence[] cs = phone.toArray(new CharSequence[phone.size()]);

            AlertDialog.Builder builder = new AlertDialog.Builder(Contact.this);

            builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            builder.setTitle("Would You Like to Call ");
            builder.setItems(cs, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {

                    call(Contact.this, cs[item].toString());

                }
            }).show();
        }

        public void call(Context ctx, String number) {
            try {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + number));
                ctx.startActivity(callIntent);
            } catch (ActivityNotFoundException activityException) {
                Log.e("helloandroid dialing example", "Call failed",
                        activityException);
            }
        }

我不知道我的代码有什么问题。请有人帮助我。

logcat的:

03-20 15:47:12.771: E/AndroidRuntime(29736): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxxxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{422a3f38 29736:com.universaltyres/10075} (pid=29736, uid=10075) requires android.permission.CALL_PHONE
 03-20 15:47:12.771: E/AndroidRuntime(29736):   at android.os.Parcel.readException(Parcel.java:1327)
 03-20 15:47:12.771: E/AndroidRuntime(29736):   at android.os.Parcel.readException(Parcel.java:1281)
 03-20 15:47:12.771: E/AndroidRuntime(29736):   at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1638)
 03-20 15:47:12.771: E/AndroidRuntime(29736):   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1385)
 03-20 15:47:12.771: E/AndroidRuntime(29736):   at android.app.Activity.startActivityForResult(Activity.java:3235)
03-20 15:47:12.771: E/AndroidRuntime(29736):    at android.app.Activity.startActivity(Activity.java:3342)
03-20 15:47:12.771: E/AndroidRuntime(29736):    at com.universaltyres.Contact.call(Contact.java:120)
03-20 15:47:12.771: E/AndroidRuntime(29736):    at com.universaltyres.Contact$2.onClick(Contact.java:111)
03-20 15:47:12.771: E/AndroidRuntime(29736):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)

3 个答案:

答案 0 :(得分:3)

您应该使用Intent.ACTION_DIAL代替Intent.ACTION_CALL。它将调出电话拨号器,就像其他应用程序的行为一样(相同的用户体验)

答案 1 :(得分:2)

您是否提供了CALL权限?

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

答案 2 :(得分:2)

你的logcat清楚地说:

3-20 15:47:12.771: E/AndroidRuntime(29736): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxxxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{422a3f38 29736:com.universaltyres/10075} (pid=29736, uid=10075) requires android.permission.CALL_PHONE

您需要在manifest.xml文件中添加以下权限

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