1

我正在获取最近的通话记录,因为通话已断开(呼出,呼入)已接听或未接听。

我正在使用电话状态侦听器在通话断开时触发广播,但是一次通话却被多次触发,为什么会这样..??

所以请告诉我如何在一次通话中只触发一次接收器。

这是我的代码

public class BroadcastReceiver  extends android.content.BroadcastReceiver{
    static boolean iscallended= true;
    Context mContext;
    TelephonyManager telephony;
     private static final String TAG = "CustomBroadcastReceiver";
    CustomPhoneStateListener customPhoneStateListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mContext = context;
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("DEBUG", state);

                telephony = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
               if(customPhoneStateListener==null)
               {

                customPhoneStateListener = new   CustomPhoneStateListener();
                telephony.listen(customPhoneStateListener,   PhoneStateListener.LISTEN_CALL_STATE);
               }



        }



    }
    private class CustomPhoneStateListener extends PhoneStateListener
    {
         private static final String TAG = "CustomPhoneStateListener";
         Handler handler=new Handler();

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            System.out.println(iscallended+  "  value of iscancelled ");
             switch (state) 
             {
             case TelephonyManager.CALL_STATE_RINGING:
                 if(!incomingNumber.equalsIgnoreCase(""))
                 {
                     //YOUR CODE HERE

                 }
                 break;

             case TelephonyManager.CALL_STATE_IDLE:
                 if(iscallended)
                 {
                     iscallended = false;
                 System.out.println("IDLE called");
                 Toast.makeText(mContext, "IDLE", Toast.LENGTH_SHORT).show();
                 Intent it = new Intent(mContext,MainActivity.class);
                 it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 mContext.startActivity(it);
                 }
                 break;
             default:
                 break;
             }
             super.onCallStateChanged(state, incomingNumber);
             telephony.listen(customPhoneStateListener, PhoneStateListener.LISTEN_NONE);
        }







    }

}

这是清单中的接收器

 <receiver android:name="com.example.calllogs.BroadcastReceiver">
            <intent-filter >

                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>


        </receiver>
4

3 回答 3

0

为避免重复触发,请使用MyPhoneStateListeneras object 和 check lastCallStatemakeMyPhoneStateListener从调用ServiceReceiver

class MyPhoneStateListener () : PhoneStateListener() {

    companion object {

        var lastCallState: Int? = null
        lateinit var context: Context

        fun makeMyPhoneStateListener(_context: Context): MyPhoneStateListener
        {
            val myPhoneStateListener = MyPhoneStateListener()
            context = _context
            return myPhoneStateListener
        }
    }

    override fun onCallStateChanged(state: Int, incomingNumber: String) {

        when (state) {
            TelephonyManager.CALL_STATE_IDLE -> {

                if (lastCallState!= TelephonyManager.CALL_STATE_IDLE){
                    // some code for CALL_STATE_IDLE
                    lastCallState = TelephonyManager.CALL_STATE_IDLE
                }
            }
            TelephonyManager.CALL_STATE_OFFHOOK -> {

                if (lastCallState!= TelephonyManager.CALL_STATE_OFFHOOK) {

                    // some code for CALL_STATE_OFFHOOK
                    lastCallState = TelephonyManager.CALL_STATE_OFFHOOK
                }
            }
            TelephonyManager.CALL_STATE_RINGING -> {
                if (lastCallState!= TelephonyManager.CALL_STATE_RINGING) {

                    // some code for CALL_STATE_RINGING
                    lastCallState = TelephonyManager.CALL_STATE_RINGING    
                }
            }    
        }    
    }
于 2020-11-08T10:14:37.380 回答
0

是的,你会明白的。

 private class MyPhoneStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            //Hangup
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("IDLE", "Call Idle" + state);

                if (isCallEnded) {
                    isCallEnded = false;
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
                                                    .setCancelable(false)
                            .setPositiveButton(getString(R.string.Yes), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    // your method
                                }
                            })
                            .setNegativeButton(getString(R.string.No), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            });
                    alertBuilder.show();
                }
                break;
            //Outgoing
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("OFFHOOK", "Call OffHook" + state);
                isCallEnded = true;
                break;
            //Incoming
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("RINGING", "Call Ringing" + state);
                break;
        }
    }
}

以上是示例,当您查看设备日志时,您肯定会看到多次摘机以及 IDLE 状态。

试着计算一下,应该没问题。

于 2015-09-09T09:55:32.040 回答
0

这是你的答案:

https://stackoverflow.com/a/5497316/3479012

还要在清单中指定访问电话状态的权限。

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

也看看这个 http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/

于 2015-09-09T06:43:12.397 回答