是否可以在android中传入和传出的调用和文本之前生成警报或任何UI

时间:2012-03-15 09:54:12

标签: android alert

在我的应用程序中,我需要生成一个警报或类似的东西,要求用户在拨出和拨入电话和短信之前进行确认。我完成了NEW_OUTGOING_CALL,我只能Toast一个消息。

2 个答案:

答案 0 :(得分: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 :(得分:0)

之前我还没有在这个领域工作,但我认为,您可以使用PhoneStateListener接口..当电话状态发生变化时,它会调用函数.... Telephony Manager也可能有用。

从我这边试试希望有所帮助,