0

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

这是我的代码:

@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(){}”

我不知道如何解决它,有人可以帮助我吗?太感谢了。

4

2 回答 2

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在前面添加等..,

并且在同一个活动中也有这个代码:

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

        }
    }
}

通话结束或取消等后返回应用程序,

于 2012-09-14T09:07:51.140 回答
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" />
于 2012-09-14T08:31:54.113 回答