飞行模式开关关闭/打开后,广播接收器在onReceive()方法中接收的时间太长

7
我已创建了一个简单的广播接收器,它工作得非常好,除了当我打开/关闭飞行模式时,使用onReceive方法接收广播消息需要近2分钟才能完成。另外一个问题是,如果我们启动应用程序,然后更改飞行模式(打开/关闭),那么它将花费很长时间才能接收到消息。 如果在启动应用程序之前发生了模式更改,则不会影响onReceive方法中接收消息所需的时间。 以下是源代码。
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.

1个回答

14
在Android系统中,广播是在内部处理的。
有时由于系统负载/重新启动/高运行时间,广播接收器需要时间来接收一些意图。
解决方法是,向发送广播的意图添加FLAG_RECEIVER_FOREGROUND标记。
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

这将会使在问题场景下广播的传送速度比以前更快。


Kushal如何在清单中添加此标志以进行广播?因为我想要快速知道互联网是否已连接。 - k_kumar
从Oreo开始,基于清单的广播存在某些限制,因此我建议从java/Kotlin端动态使用它。 - Kushal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接