2012-09-14 234 views
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,字符串)是未定義的類型新的DialogInterface.OnClickListener(){}「

我不知道如何解決它,,任何人都可以幫助我嗎?非常感謝。

+1

我可以知道你爲什麼使用arrayList嗎? – ponraj

+0

這裏是我的數組列表ArrayList arrayList = new ArrayList ();我想從實體撥打電話號碼,但它不工作 –

回答

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" /> 
+0

非常感謝你,這個代碼工作,謝謝你,真的,真的很感謝你:D –

+0

@RichardoLuis請接受你認爲有用的答案:) – Satheesh

2

1日的事情是,你必須在清單中添加的權限:

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

     } 
    } 
} 

要回應用一旦通話結束s或被取消等。,