怎么打个电话?

时间:2012-09-14 08:17:28

标签: android phone-call

当我按“呼叫商店”项目时,我想在我的应用程序中拨打电话,

这是我的代码:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    final Entity_BikeShopRepair toko = adapterShop.getItem(position);

    CharSequence[] items = { "View on Map", "Call Shop" };

    AlertDialog.Builder builder = new AlertDialog.Builder(
            Tab_Shop_Repair_ListView_Activity.this);
    builder.setTitle(toko.getShop_Name());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch (item) {
            case 0:
                Toast.makeText(Tab_Shop_Repair_ListView_Activity.this,
                        toko.getShop_Name(), Toast.LENGTH_LONG).show();
                break;
            case 1:
                arrayList(Tab_Shop_Repair_ListView_Activity.this,
                        toko.getPhone_Number());
                Intent intent = new Intent(Intent.ACTION_CALL, Uri
                        .parse(arrayList.toString()));
                startActivity(intent);

                break;
            case 2:
                break;
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

但是代码是错误的“方法arrayList(Tab_Shop_Repair_ListView_Activity,String)未定义类型new DialogInterface.OnClickListener(){}”

我不知道如何解决它,有人能帮帮我吗?非常感谢你。

2 个答案:

答案 0 :(得分:2)

首先,您必须在清单中添加权限:

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

然后在活动中使用此代码进行调用:

Intent callIntent = new Intent(Intent.ACTION_VIEW);
callIntent.setData(Uri.parse("tel:" + ph_no));
startActivity(callIntent);

此处代替Intent.ACTION_CALL更好地使用Intent.ACTION_VIEW,允许用户在确认通话前更改号码,例如在前面添加0等,

并且在同一个Activity中也有这段代码:

PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

和这堂课:

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    String LOG_TAG = "LOGGING 123";

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended,
            // need detect flag from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(LOG_TAG, "restart app");

                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }
    }
}

一旦通话结束或取消等回到申请表,

答案 1 :(得分:1)

尝试使用此代码

String number = "tel:" + toko.getPhone_Number().toString();
    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number)); 
    startActivity(callIntent);

还在清单中添加权限...

<uses-permission android:name="android.permission.CALL_PHONE" />
相关问题