Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast Receiver is taking too long to receive in onReceive() after airplane mode turned off/on

I have created a simple Broadcast Receiver and it working absolutely fine except that if i turned on/off the Air Plane Mode, it is taking nearly 2 minutes to receive the broadcasted messages using onReceive method. Another thing is only this is causing a problem if we launch the application and then if i change Air plane Mode (on/off), Then it taking too long to receive the message.

If there is Mode change before launching the application it didn't affect the time to receive the message in onReceive method.

Source code is below.

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcastreceiver.BroadcastReceiverActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <receiver android:name="MyBroadcastReceiver" >
            <intent-filter>
                <action android:name="test.intent.action.QR_CODE_RECEIVER" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

BroadcastReceiverActivity.java

public class BroadcastReceiverActivity extends Activity {

    public static String qrCodeReceiver = "test.intent.action.QR_CODE_RECEIVER";

    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button broadcastBtn = (Button) findViewById(R.id.broadcastBtn);
        broadcastBtn.setOnClickListener(new View.onClickListener(){
            public void onClick(View v){
                //TODO
                Intent intent = new Intent();
                intent.putExtra("message","Testing");
                intent.setAction(qrCodeReceiver);
                sendBroadcast(intent);
                Log.d("Test","sendBroadcasting the message ::");
            }
        });
    }
}

MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO
        Toast.makeText(context, "on receive.",Toast.LENGTH_LONG).show();
        Bundle extras = intent.getExtras();
        String state = extras.getString("message");
        Log.d("Test", "Inside MyBroadcastReceiver onReceive() state :: "+ state);
        Toast.makeText(context, state,Toast.LENGTH_LONG).show();
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BroadcastReceiverActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/broadcastBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp"
    android:text="Send The BroadCast Message" />

</RelativeLayout>

Thank you in advance.

like image 666
Ilanthirayan Paramanathan Avatar asked Nov 29 '13 10:11

Ilanthirayan Paramanathan


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

Does broadcast receiver work in background?

ACTION_BOND_STATE_CHANGED)); The alternative is to register the receiver in the AndroidManifest. This ensures that the BroadcastReceiver runs all the time, even when the app is backgrounded. I chose this option because I want my application to receive Bluetooth events even when the app isn't in use (i.e. backgrounded).

What is the time limit of broadcast receiver?

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.

Is it necessary to unregister broadcast receiver?

It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources. In this tutorial, we will make an application having two buttons to register and unregister the broadcast receiver respectively. Let's define the layout file and add two buttons.


1 Answers

In Android System, broadcast are processed internally.

Sometimes, due to system loading/ restart/ high runtime, the broadcast receiver gets time to receive some intent

Workaround is, to add Flag FLAG_RECEIVER_FOREGROUND to intent sending broadcast

intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

This will speed up the broadcast delivery much than before in problem scenario

like image 86
Kushal Avatar answered Sep 29 '22 02:09

Kushal