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

我创建了一个简单的广播接收器,它工作得很好,除非我打开/关闭空中飞机模式,使用onReceive方法接收广播消息需要将近2分钟.另一件事是,如果我们启动应用程序然后如果我更改Air plane模式(开/关),则会导致问题,然后接收消息需要很长时间.

如果在启动应用程序之前有模式更改,则不会影响在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>

先感谢您.

解决方法

在Android系统中,广播在内部处理.

有时,由于系统加载/重启/高运行时间,广播接收器有时间接收一些意图

解决方法是,将Flag FLAG_RECEIVER_FOREGROUND添加到意图发送广播

intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

这将在问题情景中比以前更快地加速广播传送

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...