Android Powerkey trigger Small app Summary

Source: Internet
Author: User

Recently, did a long press Powerkey trigger dialog then contains "shutdown", "restart", "Flight mode" three button function application. Now make a record of the application's main file for future reference:

Software version: android4.4

The source code is as follows:

Package com.heimi.power;

Import COM.HEIMI.POWER.R;
Import android.app.Activity;
Import Android.app.Service;
Import Android.content.pm.ActivityInfo;
Import Android.os.Bundle;
Import Android.os.Vibrator;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.TextView;

Import Android.util.Log;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.content.Context;
Import Android.content.ComponentName;

Import Android.app.ActivityManager;
Import Android.content.BroadcastReceiver;
Import android.provider.Settings;

public class Powerkey extends Activity implements onclicklistener{
Private ImageView Restartview, Poweroffview, Airview;
Private TextView Cancelview;
Private final int click_cancel = 1;
Private final int click_reset = 2;
Private final int click_power = 3;
Private final int click_air = 4;
Static final String ACTION = "Android.intent.action.SCREEN_OFF";
Static final String ANAME = "Powerkey";


protected Broadcastreceiver broadcastreceiver = new Broadcastreceiver () {
@Override
public void OnReceive (context context, Intent Intent) {
if (Intent.getaction (). Equals (ACTION)) {
Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service);
ComponentName cn = Am.getrunningtasks (1). Get (0). topactivity;
if (Cn.getclassname (). Contains (ANAME)) {
Context.unregisterreceiver (Broadcastreceiver);
Finish ();
}
}
}
};


@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Intentfilter filter = new Intentfilter ();
Filter.addaction (Intent.action_screen_off);
Registerreceiver (broadcastreceiver, filter, NULL, NULL);

GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Setcontentview (R.layout.main);

Airview = (ImageView) Findviewbyid (R.id.iv_dormancy_pattern);
Airview.setonclicklistener (this);
Airview.settag (Click_air);
if (Getairplanemodestatus ()) {
Airview.setimagedrawable (Getresources (). getdrawable (R.drawable.dormancy_pattern_normal));
} else {
Airview.setimagedrawable (Getresources (). getdrawable (r.drawable.dormancy_pattern_pressed));
}

Cancelview = (TextView) Findviewbyid (r.id.iv_cancel);
Cancelview.setonclicklistener (this);
Cancelview.settag (Click_cancel);

Restartview = (ImageView) Findviewbyid (R.id.iv_restart);
Restartview.setonclicklistener (this);
Restartview.settag (Click_reset);

Poweroffview = (ImageView) Findviewbyid (R.id.iv_shutdown);
Poweroffview.setonclicklistener (this);
Poweroffview.settag (Click_power);
}

@Override
public void OnStop () {
Super.onstop ();
}

@Override
public void Onresume () {
Super.onresume ();
}

@Override
public void Onrestart () {
Super.onrestart ();
}

Get airplane mode off or on
Private Boolean getairplanemodestatus () {
Boolean status = Settings.Global.getInt (This.getcontentresolver (),
Settings.Global.AIRPLANE_MODE_ON, 0) = = 1? True:false;

return status;
}

Turn airplane mode on or off
private void Setairplanemode (context context, Boolean enable)
{
Settings.Global.putInt (Context.getcontentresolver (),
Settings.Global.AIRPLANE_MODE_ON, enable? 1:0);
Intent airintent = new Intent (intent.action_airplane_mode_changed);
Airintent.putextra ("state", enable);
Context.sendbroadcast (airintent);
}

public void OnClick (View v) {
int tag = (Integer) v.gettag ();
Switch (TAG) {
Case Click_cancel:
This.finish ();
Break
Case Click_reset:
Intent resetintent = new Intent (intent.action_reboot);
Resetintent.putextra ("nowait", 1);
Resetintent.putextra ("Interval", 1);
Resetintent.putextra ("window", 0);
Sendbroadcast (resetintent);
Break
Case Click_power:
Intent powerintent = new Intent (Intent.action_request_shutdown);
Powerintent.putextra (intent.extra_key_confirm, false);
Where false is changed to true, the confirmation window will pop up whether the shutdown is
Powerintent.setflags (Intent.flag_activity_new_task);
StartActivity (powerintent);
Break
Case Click_air:
if (Getairplanemodestatus ()) {
Setairplanemode (this, false);
Airview.setimagedrawable (Getresources (). getdrawable (r.drawable.dormancy_pattern_pressed));
} else {
Setairplanemode (this, true);
Airview.setimagedrawable (Getresources (). getdrawable (R.drawable.dormancy_pattern_normal));
}
Break

}
}
}

O (∩_∩) o~, .... Come to an end and be continued!

Android Powerkey trigger Small app Summary

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.