BroadcastReceiver只能工作一次

时间:2014-04-10 14:02:50

标签: android broadcastreceiver call

我有一个扩展BroadcastReceiver的类。此类检测拨出呼叫,特别是告诉我呼叫的开始和结束时间。关键是当我第一次运行应用程序时它没有问题,随附的toast告诉我调用的状态。关键是当我在应用程序突然关闭后几秒或几分钟进行新的调用时。我在相应的清单上设置了权限。类代码是下一个:

 public class OutgoingCallReceiver extends BroadcastReceiver {
    private Context mContext;
    private Intent mIntent;
    private boolean isPhoneCalling = false;
    private String TAG="PhoneStateListener";

    @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) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.i(TAG, "RINGING, number: " + incomingNumber);
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.i(TAG, "OFFHOOK");
                Toast.makeText(mContext, "Llamada saliente repicando", Toast.LENGTH_LONG).show();
                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // run when class initial and phone call ended,
                // need detect flag from CALL_STATE_OFFHOOK
                Log.i(TAG, "IDLE");
                if (isPhoneCalling) {
                    Log.i(TAG, "TERMINO LA LLAMADA =D");
                    Toast.makeText(mContext, "Llamada saliente finalizada", Toast.LENGTH_LONG).show();
                    isPhoneCalling = false;
                }

            }
    }

    };

}

在清单中我有以下内容:

    <receiver android:name=".OutgoingCallReceiver">   
            <intent-filter>                
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
    </receiver>   

   <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
  <uses-permission android:name="android.permission.SEND_SMS"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

错误是从第二次运行开始,应用程序突然结束。我无法确定错误。任何帮助表示赞赏。问候。

0 个答案:

没有答案