在清单中注册Broadcastreceiver [英] Register Broadcastreceiver in manifest

查看:76
本文介绍了在清单中注册Broadcastreceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下manifest.xml:

I have the following manifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name=".PhoneStateBroadcastReceiver"
        android:permission="android.permission.READ_PHONE_STATE">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

侦听器在主要活动运行时接收数据.但是,当主要活动被杀死时,接收器停止输出信息.

The listener receives data while the main activity is running. However when the main activity is killed, the receiver stops to output information.

接收器是一个示例类:

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "PhoneReceiver";
    Context mContext;
    String incoming_number;
    private int prev_state;

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager

        Bundle bundle = intent.getExtras();
        String phoneNr = bundle.getString("incoming_number");
        Log.v(TAG, phoneNr);
        Log.d( TAG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
        mContext = context;
    }

    /* Custom PhoneStateListener */
    public class CustomPhoneStateListener extends PhoneStateListener {

        private static final String TAG = "CustomStateListener";

        @Override
        public void onCallStateChanged(int state, String incomingNumber){

            if( incomingNumber != null && incomingNumber.length() > 0 )
                incoming_number = incomingNumber;

            switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "CALL_STATE_RINGING");
                    prev_state=state;
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d(TAG, "CALL_STATE_OFFHOOK");
                    prev_state=state;
                    break;

                case TelephonyManager.CALL_STATE_IDLE:

                    Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number);

                    if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
                        prev_state=state;
                        //Answered Call which is ended
                    }
                    if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                        prev_state=state;
                        //Rejected or Missed call
                    }
                    break;
            }
        }
    }
}

据我了解的android原理,当接收方在清单中注册时,即使其应用程序被杀死,接收方也应接收数据,如果是的话,我在做什么错了?

As far as I understood the android principles, when a receiver is registered in manifest then it should receive data even if its app is killed, if so, what am I doing wrong?

推荐答案

确定.在将头撞墙超过一个星期后,我决定创建一个功能最少的新项目,该项目从第一次尝试就开始起作用.我不知道它是由旧代码中的某些内容引起的,对此我表示怀疑,因为我将其逐个方法复制到新项目中并且仍然可以使用,或者是新版本的Android Studio及其构建工具.

OK. After banging my head against the wall for more than a week, I decided to create a new project with the minimum functionality and it worked from the first try. I don't know whether it was caused by something in my old code, which I doubt, since I copied it method by method to the new project and it still works, or the newer versions of Android Studio and their build tools.

非常感谢REG1的耐心和表达想法的渴望.

Huge thanks to REG1 for having the patience and desire to give ideas.

致谢

这篇关于在清单中注册Broadcastreceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆