Android broadcasts a broadcast in the broadcast receiver Broadcastreceiver, causing the current broadcast receiver to continually loop through the problem

Source: Internet
Author: User

Recently in the company processing projects, using the lock screen status popup activity to prompt, similar to QQ message popup function. When used, if the popup activity is already located outside the lock screen, will not play the window again, but send the broadcast to notify the data update, but in the broadcast receiver in a step to send a notification again operation, found that the broadcast receiver becomes a dead loop execution.  After careful examination, it was found that there was no wrong action in any place. Finally, the broadcast in the receiver to send the broadcast statement comments, the dead loop is not. The code is as follows:

Note: All the actions inside are carefully checked, used correctly, with no errors, or reference errors.

Package com.guc.doctorwarn.receiver;

Import com.guc.doctorwarn.LocalScreenMessageActivity;
Import Com.guc.doctorwarn.utils.WakeLock;
Import Com.library.helper.AppIsBackground;
Import Com.library.util.PreferencesUtil;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.util.Log;

/**
* This broadcast is used to receive the lock screen prompt broadcast
*
* @author Administrator
*
*/

public class Localscreenmessagereceiver extends Broadcastreceiver {

public static final String localscreenmessageaction = "Com.guc.doctorwarn.LocalScreenMessageAction";

@Override
public void OnReceive (context context, Intent Intent) {

/**
* If the app is in the background, unlock the ScreenTip or do not prompt
*/

LOG.I ("A", ">>> lock screen message broadcast receiver received message <<<");

LOG.I ("a",
">>> whether the app is in the background <<<"
+ Appisbackground
. Isapplicationbroughttobackground (context));
if (Intent.getaction (). Equals (Localscreenmessageaction)) {
if (Appisbackground.isapplicationbroughttobackground (context)
&&! Preferencesutil.getboolean (Context, "screen")) {

Wakelock.acquirecpuwakelock (context);

Intent localmessage = new Intent ();
Localmessage
. SetClass (context, localscreenmessageactivity.class);
Localmessage.setflags (Intent.flag_activity_clear_top);
Localmessage.addflags (Intent.flag_activity_new_task);
Context.startactivity (Localmessage);

} else if (! Appisbackground
. Isapplicationbroughttobackground (context)
&& Preferencesutil.getboolean (context, "screen")) {

Intent updatamessage = new Intent ();
Updatamessage
. Setaction (Localscreenmessageactivity.uplocalmessagedataaction);
Context.sendbroadcast (Intent);
//
LOG.I ("A", "Broadcast update lock screen activity execution");

}
}

}
}

Summary: In the Android development process, the broadcast receiver can not send the broadcast again, or the operation of the broadcast receiver will enter a dead loop state.

Android broadcasts a broadcast in the broadcast receiver Broadcastreceiver, causing the current broadcast receiver to continually loop through the problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.