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

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

如果在启动应用程序之前有模式更改,则不会影响在onReceive方法中接收消息的时间.

源代码如下.

AndroidManifest.xml中

package="com.example.broadcastreceiver"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="18" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.broadcastreceiver.BroadcastReceiverActivity"

android:label="@string/app_name" >

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中

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" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

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" />

先感谢您.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值