即使在一个注册后,Android BroadcastReceiver onReceive()在android 5.1.1上调用了两次 [英] Android BroadcastReceiver onReceive() called twice on android 5.1.1 even after one register

查看:148
本文介绍了即使在一个注册后,Android BroadcastReceiver onReceive()在android 5.1.1上调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚下面的代码出了什么问题。我还检查了两次注册接收器。但事实并非如此。还是我想念一些东西。
请帮忙。我真的很需要:(

I Could not figure out what is wrong with below code. I also checked about registering receiver twice. But that's also not the case. or may be I am missing something. Could any please help. I really need it. :(

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author Bharat
 *
 */
public class CallNotifierService extends Service 
{
    private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
    private CallBr br_call;

    @Override
    public IBinder onBind(Intent arg0) 
    {
        return null;
    }

    @Override
    public void onDestroy() 
    {
        Log.d("service", "destroy");
        this.unregisterReceiver(this.br_call);
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_OUT);
        filter.addAction(ACTION_IN);
        this.br_call = new CallBr();
        this.registerReceiver(this.br_call, filter);
        return START_NOT_STICKY;
    }

    public class CallBr extends BroadcastReceiver 
    {
        Bundle bundle;
        String state;
        String inCall, outCall;
        public boolean wasRinging = false;
        public boolean answered = false;
        public boolean outgoing = false;

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            if (intent.getAction().equals(ACTION_IN)) 
            {
                if ((bundle = intent.getExtras()) != null) 
                {
                    state = bundle.getString(TelephonyManager.EXTRA_STATE);

                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
                    {
                        inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        wasRinging = true;
                        Toast.makeText(context, "Incoming Call : " + inCall, Toast.LENGTH_LONG).show();
                    } 
                    else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
                    {
                        if (wasRinging == true) 
                        {
                            answered = true;
                            Toast.makeText(context, "Answered", Toast.LENGTH_LONG).show();
                        }
                    } 
                    else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) 
                    {
                        wasRinging = false;
                        Toast.makeText(context, "Disconnected", Toast.LENGTH_LONG).show();
                    }
                }
            } 
            else if (intent.getAction().equals(ACTION_OUT)) 
            {
                if ((bundle = intent.getExtras()) != null) 
                {
                    outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    Toast.makeText(context, "Outgoing Call : " + outCall, Toast.LENGTH_LONG).show();
                    outgoing = true;
                }
            }
        }
    }
}

下面是我从中调用的活动

Below is the activity I am calling it from

public class MyActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.roaming);

    Intent intent = new Intent(this, CallNotifierService.class);
    startService(intent);
}
.
.
.
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ind.cosmos.main"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

    <uses-sdk
        android:minSdkVersion="22"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <!-- android:theme="@style/AppTheme"> -->
        <activity
            android:name="ind.cosmos.main.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="ind.cosmos.callRecord.CallNotifierService" />
    </application>

推荐答案

实际上..代码没有错。触发它的系统。有时是2倍,而其他时候是4倍。

Actually .. there is nothing wrong with the code. its System who triggers this. some time its 2 times.. other times it touches 4 times.

所以我们最好的办法是在代码本身中处理它。

So the best we can do it handle it in the code itself.

这篇关于即使在一个注册后,Android BroadcastReceiver onReceive()在android 5.1.1上调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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