android powerkey觸發小應用總結

來源:互聯網
上載者:User

標籤:android4.4   app   power   應用   軟體   

        近日,做了一個長按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~,。。。。先告一段落,未完待續!

android powerkey觸發小應用總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.