2012-03-15 72 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也许也是有用的。

从我的身边,希望帮助只是一种尝试,