1

通話が切断された (発信、着信) 応答または未応答のいずれかとして、最近の通話ログの取得に取り組んでいます。

電話状態リスナーを使用して、通話が切断されたときにブロードキャストを起動していますが、1回の通話で複数回起動されるのはなぜですか..??

1回の通話で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

これがあなたの答えです:

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 に答える