发生某些事件时,将手机状态更改为响铃

时间:2012-12-07 06:36:31

标签: android broadcastreceiver telephony

我正在开发一个应用程序,我必须将手机状态更改为响铃,因为有些号码正在我的手机上调用。那可能吗?我对广播接收器有一些想法,它会将该事件广播到我的应用程序,然后我的应用程序将在我的手机上进行任何虚拟假号码呼叫。请给我任何想法。可能吗?以及如何开始呢?

感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:1)

要更改为振铃,请使用TelephonyManager,AudioManager和PhoneStateListener:

{
  // USE AudioManager  for Settingringing from vibration
         AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);   

         switch (am.getRingerMode()) {   
                 case AudioManager.RINGER_MODE_VIBRATE:   
                 Log.i("MyApp","NORMAL mode");
                 am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
              break;   
            }     

答案 1 :(得分:0)

您可以添加此代码,以便在触发某个事件时拨打电话。

      String Numb = "tel:" + "your number";
      Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(Numb));
      startActivity(intent);

并为您的清单添加权限。

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

答案 2 :(得分:0)

尝试这样的事情

public class Telephone extends BroadcastReceiver {

    private Context mContext;
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
        TelephonyManager tm = (TelephonyManager)              
                 context.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;
        tm.listen(phoneStateListener, events);
    }

    private final PhoneStateListener phoneStateListener = 
                                                new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            String callState = "UNKNOWN";
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:

                break;
            case TelephonyManager.CALL_STATE_RINGING:
                                // DO YOUR WORK HERE

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                          if(incomingNumber.length() >0){

                                  // incoming call
                                 // DO YOUR WORK HERE when call is received
                                 // SEND SMS HERE
                            }
                           else{
                                      // outgoing call
                              }
                break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    };

}

您需要注册一个PhoneStateListener.LISTEN_CALL_STATE才能接收

BroadcastReceiver内的来电。