android powerkey触发小应用总结
近日,做了一个长按powerkey触发dialog然后包含“关机”,"重启",“飞行模式”三个button功能的应用。现将该应用的主文件做一下记录,方便日后查阅:
软件版本:android4.4
//源码如下:
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();
}
//获取飞行模式关闭或开启状态
private boolean getAirplaneModeStatus() {
boolean status = Settings.Global.getInt(this.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) == 1 ? true : false;
return status;
}
//开启或关闭飞行模式
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);
//其中false换成true,会弹出是否关机的确认窗口
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~,。。。。先告一段落,未完待续!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。