1

我正在使用broadcastreceiver檢測呼出並在撥號開始後啓動服務。服務已啓動。現在我想在通話結束或撥號結束時停止服務。在廣播接收器中檢測呼出結束狀態

@Override 
public void onReceive(Context context, Intent intent) { 

     if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { 

     intent = new Intent(context, serv.class); 
     context.startService(intent); 
    } 

    if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_IDLE)) { 
     intent = new Intent(context, serv.class); 
     context.stopService(intent); 

    } 
} 

回答

0
try { 
       //to Fix Lolipop broadcast issue. 
       long subId = intent.getLongExtra("subscription", Long.MIN_VALUE); 
       if (subId < Integer.MAX_VALUE) { 
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
         savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); 
        } else { 
         String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
         String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         int state = 0; 
         if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
          state = TelephonyManager.CALL_STATE_IDLE; 

         } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
          state = TelephonyManager.CALL_STATE_OFFHOOK; 

         } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
          state = TelephonyManager.CALL_STATE_RINGING; 
         } 
         onCallStateChanged(context, state, number); 
        } 
       } 
      } catch (Exception e) { 

       e.printStackTrace(); 
      } 


public void onCallStateChanged(final Context context, int state, String number) { 
      switch (state) { 
       case TelephonyManager.CALL_STATE_RINGING: 
        isIncoming = true; 
        callStartTime = new Date(); 
        savedNumber = number; 


        TimerTask tt = new TimerTask() { 
         @Override 
         public void run() { 

          onIncomingCallStarted(context, savedNumber, callStartTime); 

         } 
        }; 
        Timer t = new Timer(); 
        t.schedule(tt, 1000); 
//     TimerTask tt = new TimerTask() { 
//      @Override 
//      public void run() { 
//      } 
//     }; 
//     onIncomingCallStarted(context, savedNumber, callStartTime); 
        break; 
       case TelephonyManager.CALL_STATE_OFFHOOK: 
        if (lastState == TelephonyManager.CALL_STATE_RINGING) { 
         isIncoming = true; 
         callStartTime = new Date(); 
         onReceiveCall(context, savedNumber, callStartTime, "received"); 
        } else { 
         isIncoming = false; 
        } 
        break; 
       case TelephonyManager.CALL_STATE_IDLE: 
        if (lastState == TelephonyManager.CALL_STATE_RINGING) { 

         onMissedCall(context, savedNumber, callStartTime, "Missed"); 
        } else if (isIncoming) { 
         onIncomingCallEnded(context, savedNumber, callStartTime, new Date()); 
        } else { 
         onOutgoingCallEnded(context, savedNumber, callStartTime, new Date(), "outgoing"); 
        } 
        break; 
      } 
     lastState = state; 

    } 
相關問題