How Android looks at the battery charge (based on Broadcastreceiver) _android

Source: Internet
Author: User
Tags stub

The example in this article describes how Android looks at the battery charge. Share to everyone for your reference, specific as follows:

The procedure is as follows:

Import android.app.Activity;
Import Android.app.Dialog;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.Button;
Import Android.widget.TextView;
 public class A02activity extends activity {private int level;
 private int scale;
 Private Button B01; Private Broadcastreceiver mbatinforeceiver=new Broadcastreceiver () {@Override public void onreceive Tent Intent) {//TODO auto-generated method Stub String action=intent.getaction (); Onbatteryinforeceiver (); Displays the power in a new window if (Intent.ACTION_BATTERY_CHANGED.equals) {Level=intent.getintextra ("
  Level ", 0);
  Scale=intent.getintextra ("scale", 100); Onbatteryinforeceiver (Level,scaLe);
  }
 }
 }; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    b01= (Button) Findviewbyid (R.ID.BUTTON01);
    B01.setbackgroundcolor (Color.green);
    B01.settext ("View charge"); B01.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
  Stub registerreceiver (mbatinforeceiver,new Intentfilter (intent.action_battery_changed));
  }
    });
   } public void Onbatteryinforeceiver (int intlevel,int intscale) {final Dialog d=new Dialog (a02activity.this);
   D.settitle (R.string.str_title);
   D.setcontentview (R.layout.dialog);
   Window Window=d.getwindow ();
   Window.setflags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
   TextView mtextview02= (TextView) D.findviewbyid (R.ID.MYTEXTVIEW02); The battery charge is displayed in the dialog mtextview02.settext (GetresouRCEs (). GetText (R.string.str_body) +string.valueof (intlevel*100/intscale) + "%");
   Button b02= (button) D.findviewbyid (R.ID.BUTTON02);
   B02.setbackgroundcolor (color.red);
   B02.settext ("return");
   B02.settextcolor (Color.yellow); B02.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//Register receiver and close the window unregist
  Erreceiver (Mbatinforeceiver);
  D.dismiss ();
   }
   });
  D.show ();

 }
}

In Android, Android.intent.BATTERY_CHANGED is the system's broadcast action message, and the system broadcasts the action when the battery is in a charging state or when the battery changes. Broadcastreceiver in the program when registering, because the Set intent filter filter This action information, so when Broadcastreceiver one is registered, can immediately capture this action, and then obtain the battery charge.
The Onreceiver () in the main program is the method that will run when the broadcastreceiver is triggered, as follows:

public void Onreceiver (context context,intent Intent) {
   String action=intent.getaction ();
   if (Intent.ACTION_BATTERY_CHANGED.equals (ACTION)) {/
      * code to Run program/
   }
}

Adding this judgment Intent.ACTION_BATTERY_CHANGED.equals (ACTION) is to ensure that the broadcastreceiver will only be triggered by intent.action_battery_changed. If you do not have this judgment program can also be run.

The Android API explains that to register a receiver that contains intent.action_battery_changed, it can only be registered in the program in Context.registerreceiver (). Cannot register directly in Androidmanifest.xml.

In this example, the window of the background is rendered ambiguous when the dialog is ejected:

Final Dialog d=new Dialog (a02activity.this);
D.settitle (r.string.str_title);
D.setcontentview (r.layout.dialog);
Window Window=d.getwindow ();
Window.setflags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Where WindowManager.LayoutParams.FLAG_BLUR_BEHIND is telling the current window whatever the object is displayed on the front end, it will appear on the top of the window, leaving the background window blurry. You can also use this effect in other programs.

More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"

I hope this article will help you with the Android program.

Related Article

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.