在android中调用结束后返回上一个模式

时间:2015-07-02 11:21:56

标签: android broadcastreceiver silent phone-state-listener incoming-call

在我的应用程序中我想添加一个选项:当一组特定号码调用时将手机置于静音模式(我稍后使用数据库,现在尝试使用数字)

这是我的代码:

public class PhoneCallReceiver extends BroadcastReceiver {
 Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony  telephonyService;

 private String silentNum="+.....";
 AudioManager am,temp;

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.v(TAG, "Receving....");
  TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
      am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      temp = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      temp.getRingerMode();

   Class c = Class.forName(telephony.getClass().getName());
   Method m = c.getDeclaredMethod("getITelephony");
   m.setAccessible(true);
   telephonyService = (ITelephony) m.invoke(telephony);
   Bundle bundle=intent.getExtras();
   String phoneNumber = bundle.getString("incoming_number");
   db= new dbhandler(context);
   db.open();
   int ch = db.checkexist("call", phoneNumber, "1");
   if ((phoneNumber != null) && (ch==1)){
   //telephonyService.silenceRinger();
   telephonyService.endCall();
   Log.e("HANG UP", phoneNumber);
   }

   if ((phoneNumber != null) && (phoneNumber.equals(silentNum))){
       am.setRingerMode(0);
       }

       }

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

我也将BroadcastReceiver添加到我的清单中,现在当其中一个号码呼叫时,手机进入静音模式。 我的问题是我不知道如何取回以前的手机模式,我不知道通话何时结束

这是回到以前模式的正确代码吗?

我应该把它放在哪里?

   switch (temp.getRingerMode()) {
   case AudioManager.RINGER_MODE_SILENT:
   Log.i("MyApp","Silent mode");
   break;
   case AudioManager.RINGER_MODE_VIBRATE:
   Log.i("MyApp","Vibrate mode");
   break;
   case AudioManager.RINGER_MODE_NORMAL:
   Log.i("MyApp","Normal mode");
   break;

1 个答案:

答案 0 :(得分:0)

试试这个

if ((phoneNumber != null) && (phoneNumber.equals(silentNum))){
       am.setRingerMode(0);
       }
else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals
                    (TelephonyManager.EXTRA_STATE_IDLE)) {
                // This code will execute when the call's disconnected
                Toast.makeText(context, "You Hang up",
                        Toast.LENGTH_SHORT).show();
                AudioManager maudio = (AudioManager) context.
                        getSystemService(context.AUDIO_SERVICE);
                maudio.setRingerMode
                        (AudioManager.RINGER_MODE_NORMAL);

            }