广播接收器的时间过长,以收到的onReceive()后,飞行模式关闭/开启 [英] Broadcast Receiver is taking too long to receive in onReceive() after airplane mode turned off/on

查看:599
本文介绍了广播接收器的时间过长,以收到的onReceive()后,飞行模式关闭/开启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的广播接收器和它的工作只是如果我打开/关闭飞行模式,它走了近2分钟,接收使用的onReceive方法广播消息精绝。另一件事是只有这导致一个问题,如果我们启动应用程序,然后如果我改变飞行模式(开/关),然后它太长收到消息。

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.

如果有启动应用程序前的模式改变并没有影响到接收的onReceive方法的消息的时间。

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

来源$ C ​​$ C如下。

Source code is below.

的Andr​​oidManifest.xml

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

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

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

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>

感谢您提前。

推荐答案

在Android系统,广播的内部处理。

In Android System, broadcast are processed internally.

有时,由于系统负载/重启/高运行时,广播接收器获取的时间来接受一些意图

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

解决方法是,以标志FLAG_RECEIVER_FOREGROUND添加到意向发送广播

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

这篇关于广播接收器的时间过长,以收到的onReceive()后,飞行模式关闭/开启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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