Richardo Luis
Richardo Luis

Reputation: 37

how to make a phone call?

i want to make a phone call in my apps when i press item "call shop",

here's my code:

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

but the code is error with "The method arrayList(Tab_Shop_Repair_ListView_Activity, String) is undefined for the type new DialogInterface.OnClickListener(){}"

i dont know how to fix it,, can anybody help me? thank you so much.

Upvotes: 0

Views: 371

Answers (2)

Archie.bpgc
Archie.bpgc

Reputation: 24012

1st thing is you must add the permission in the manifest:

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

then in the activity use this code to make a call:

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

Here instead of Intent.ACTION_CALL better use Intent.ACTION_VIEW, to allow user to change the number before confirming the call, like adding 0 in front etc..,

and also in the same Activity have this code:

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

and this class:

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

        }
    }
}

to get back to application once the call ends or gets cancelled etc..,

Upvotes: 2

ponraj
ponraj

Reputation: 768

Try this code it will work

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

Also Add Permission in manifest...

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

Upvotes: 1

Related Questions