2012-03-15 50 views
0

在我的應用程序需要生成一個警告或實物,詢問用戶確認之前傳出和傳入的電話和短信的東西呼入和呼出電話和短信的任何UI。我完成了NEW_OUTGOING_CALL,我可以只爲Toast a msg。是否有可能產生警報或之前的Android

+0

解釋更多你想要做什麼,你做了什麼。一般的路徑是使用廣播接收器。但是當用戶有一些其他行動要快速完成時顯示自定義用戶界面通常是一個非常非常糟糕的主意。 – Snicolas 2012-03-15 10:03:26

+0

我只是想讓用戶重新確認他是否真的想打電話給某些特定號碼(應用程序最初從用戶那裏獲取),並且我已經使用broadcastreceiver在打出的電話上打開我的應用程序,但我無法暫停呼叫過程......請幫助我。 – Kishore 2012-03-15 10:58:20

回答

1

您可以使用ITelephone.aidl和發生的事件來聽電話的狀態。我正在幫助您處理電話呼叫(包括關於如何斷開它的簡短摘錄)。 SMS也以類似的方式發生。這將是最好的,如果你可以研究並找出它

import com.android.internal.telephony.ITelephony; 
import android.os.IBinder; 
import android.widget.Toast; 
import android.telephony.TelephonyManager; 
import android.telephony.PhoneStateListener; 

public class CallMonitor 
{ 
    protected StateListener phoneStateListener; 
    //stops the service to monitor any call. 
    public void stopMonitor() 
    { 
     try 
     { 
      TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
      telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); 
      phoneStateListener=null; 
      Toast.makeText(this, "Call Monitoring Stopped", Toast.LENGTH_SHORT).show(); 
     } 
     catch (Exception e) 
     { 

     }  
    } 

    public void startMonitor() 
    { 
     try 
     { 
      phoneStateListener = new StateListener(); 
      TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
      telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
      Toast.makeText(this, "Call Monitoring Started", Toast.LENGTH_SHORT).show();    
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     return null; 
    } 


    class StateListener extends PhoneStateListener 
    { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) 
     { 
      super.onCallStateChanged(state, incomingNumber); 
      switch(state) 
      { 
       case TelephonyManager.CALL_STATE_RINGING: 

         Context context = getApplicationContext(); 
         try 
         { 
          TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
          Class c = Class.forName(manager.getClass().getName()); 
          Method m = c.getDeclaredMethod("getITelephony"); 
          m.setAccessible(true); 
          ITelephony telephony = (ITelephony)m.invoke(manager); 
          telephony.endCall(); 
         } 
         catch(Exception e) 
         { 
          Toast.makeText(context, "Error ending the call" + e.getMessage(), Toast.LENGTH_LONG).show(); 
         } 
        break; 
       case TelephonyManager.CALL_STATE_OFFHOOK: 
        break; 
       case TelephonyManager.CALL_STATE_IDLE: 
        break; 
      } 
     } 
    }; 
} 

權限是

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
+1

非常感謝你的幫助紀州..但它已經制定了更有效地利用廣播接收器.. – Kishore 2012-03-19 11:34:48

+0

謝謝你讓我知道@Kishore,你能幫助我瞭解究竟是什麼讓你相信的廣播接收器的效率。在我的應用程序中,這段代碼讓手機響了1/4秒,然後處理它。你觀察到了什麼? – kishu27 2012-03-19 16:22:02

+0

對不起延遲迴復...實際上BroadcaseReceiver的過程是執行操作之前的意圖將廣播相應的消息。因此,在執行操作之前觸發BroadcastReceiver ..因此,在振鈴之前,我可以處理呼叫。 – Kishore 2012-03-20 11:29:03

0

我以前沒有在這方面工作過,但我認爲,您可以使用PhoneStateListener接口..它將調用功能作爲手機狀態更改.... Telephony Manager也許也是有用的。

從我的身邊,希望幫助只是一種嘗試,

相關問題