Android Source Explorer methods for customizing the Android shutdown Interface _android

Source: Internet
Author: User

This article is an example of the Android Open-source approach to customizing the Android shutdown interface. Share to everyone for your reference. Specifically as follows:

In the Android system, a long press power key by default will eject the dialog box to allow you to choose "Flight Mode", "mute", "shutdown" and other functions. As shown in the following illustration:

But these features are not necessary for android-x86 and other end products. This article simply describes how to customize the Shutdown interface.

My goal is to press the power key long and it will shut down and eject the "device will shutdown" selection dialog box. If you can select "Yes" to shut down, and "no" to return to the system.

As mentioned in the Android Source customization essentials, first you have a thorough understanding of the entire system and find the code that pops up the original selection box, which is here:

<pre name= "code" class= "Java" >frameworks\policies\base\phone\com\android\internal\policy\impl\ Phonewindowmanager.java
The code that displays the dialog box call is as follows:

Runnable mpowerlongpress = new Runnable () {public
 void run () {
  mshouldturnoffonkeyup = false;
  PERFORMHAPTICFEEDBACKLW (null, hapticfeedbackconstants.long_press, false);
  Sendclosesystemwindows (system_dialog_reason_global_actions);
  Showglobalactionsdialog ();
 }
;

The Showglobalactionsdialog method is called to converge to a dialog box with options such as flight mode, mute, shutdown, and so on.

Find this place and we'll know what to do! Kill it, replace it with the shutdown code we want, and it's done! In this way, without further ado, let's go to the Showgloabalactiondialog method to see where the shutdown part is!

The implementation section of the Showglobalactionsdialog is here:
Frameworks\policies\base\phone\com\android\internal\policy\impl\globalaction.java

public void ShowDialog (Boolean keyguardshowing, Boolean isdeviceprovisioned) {
 mkeyguardshowing = keyguardshowing; 
 mdeviceprovisioned = isdeviceprovisioned; 
 if (Mdialog = = null) {
  Mstatusbar = (statusbarmanager) mcontext.getsystemservice (context.status_bar_service);
  Mdialog = Createdialog ();
 } 
 Preparedialog (); 
 Mstatusbar.disable (Statusbarmanager.disable_expand);
 Mdialog.show (); 
}

We can see very clearly, here is a new mdialog, then prepare then show it, then, this mdialog is the key to see how it was createdialog created it, still in this file:

/** * Create the Global Actions dialog. 
 * @return A New dialog. * Private Alertdialog Createdialog () {msilentmodetoggle = new toggleaction (R.drawable.ic_lock_silent_mode, R . Drawable.ic_lock_silent_mode_off, R.string.global_action_toggle_silent_mode, R.string.global_action_silent_mode _on_status, r.string.global_action_silent_mode_off_status) {void Willcreate () {//XXX:FIXME:switch to Ic_lo Ck_vibrate_mode when available Menablediconresid = (Settings.System.getInt (Mcontext.getcontentresolver (), Settin Gs. System.vibrate_in_silent, 1) = = 1)? 
  R.drawable.ic_lock_silent_mode_vibrate:r.drawable.ic_lock_silent_mode; } void Ontoggle (Boolean on) {if (on) {Maudiomanager.setringermode (Settings.System.getInt (mcontext.getcontent Resolver (), Settings.System.VIBRATE_IN_SILENT, 1) = = 1)? 
   AudioManager.RINGER_MODE_VIBRATE:AudioManager.RINGER_MODE_SILENT); else {Maudiomanager.setringermode (AudioManager.ringer_mode_normal); 
  public Boolean Showduringkeyguard () {return true; 
  public Boolean showbeforeprovisioning () {return false;
 } 
 }; Mairplanemodeon = new Toggleaction (R.drawable.ic_lock_airplane_mode, R.drawable.ic_lock_airplane_mode_off, R. String.global_actions_toggle_airplane_mode, R.string.global_actions_airplane_mode_on_status, R.string.global_ Actions_airplane_mode_off_status) {void Ontoggle (Boolean on) {if Boolean.parseboolean (systemproperties.get (Telephonyproperties.property_inecm_mode)))
    {miswaitingforecmexit = true; Launch ECM Exit Dialog Intent ecmdialogintent = new Intent (telephonyintents.action_show_notice_ecm_block_oth 
    ERS, NULL);
    Ecmdialogintent.addflags (Intent.flag_activity_new_task);
   Mcontext.startactivity (ecmdialogintent);
   else {changeairplanemodesystemsetting (on); }} @Override protected void Changestatefrompress (Boolean Buttonon) {//InECM mode airplane State cannot is changed if (!) ( Boolean.parseboolean (Systemproperties.get (Telephonyproperties.property_inecm_mode))) {mState = ButtonOn?
    STATE.TURNINGON:STATE.TURNINGOFF;
   Mairplanestate = mstate;
  public Boolean Showduringkeyguard () {return true;
  public Boolean showbeforeprovisioning () {return false;
 }
 }; <span style= "color: #ff0000;"
   >mitems = lists.newarraylist (//silent mode msilentmodetoggle,//Next:airplane mode Mairplanemodeon, Last:power off New Singlepressaction (Com.android.internal.r.drawable.ic_lock_power_off, R.string.global_ Action_power_off) {</span><span style= "color: #3333ff;" 
     ><u>public void Onpress () {//shutdown by making sure radio and power are handled.
    Shutdownthread.shutdown (Mcontext, true); }</u></span><span style= "color: #ff0000;"
   > Public boolean Showduringkeyguard () {return true; public Boolean showbeforeprovisioning () {return true;
 }</span>});
 Madapter = new Myadapter ();
 Final Alertdialog.builder ab = new Alertdialog.builder (mcontext);
 Ab.setadapter (Madapter, this). Setinversebackgroundforced (True). Settitle (r.string.global_actions);
 Final Alertdialog Dialog = Ab.create ();
 Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); if (!mcontext.getresources (). Getboolean (Com.android.internal.r.bool.config_sf_slowblur)) {Dialog.getWindow ().
 SetFlags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
 } dialog.setondismisslistener (this);
return dialog;

 }

Look what we found!! The blue part is the function that the shutdown calls!! The second parameter of the Shutdown method identifies whether the query dialog box pops up. You can choose to need (true) or do not need (false). Here I conservative a little, or choose a true, in case of accidentally press the shutdown button, hehe ...

In other words, as long as we use

Copy Code code as follows:
Shutdownthread.shutdown (Mcontext, true);

Replace the front
Copy Code code as follows:
Showglobalactionsdialog ();

You're done! What are you waiting for! We modify
Frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java
The source code is as follows:

Runnable mpowerlongpress = new Runnable () {public 
 void run () { 
  mshouldturnoffonkeyup = false; 
  PERFORMHAPTICFEEDBACKLW (null, hapticfeedbackconstants.long_press, false); 
  Sendclosesystemwindows (system_dialog_reason_global_actions); 
  Showglobalactionsdialog (); 
  Shutdownthread.shutdown (Mcontext, True); 
 } 
;

All right, it's done!!

Is that it? Discovery Compilation But ...

Details are important!!

The original Shutdownthread.shutdown (Mcontext, true) reference package did not add in!! Fortunately, gcc ...

Copy Code code as follows:
Import Com.android.internal.app.ShutdownThread;

Add the above bag to the

Frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java

, compiled again, passed, yes!

Take a look at our victories:

Do you feel the thrill and the sense of accomplishment of the source code customization?

This is just the beginning, the show is still behind!! Ha ha

I hope this article will help you with your 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.