工模-控制飞行模式状态

工模测试中需要频繁的进行飞行模式的打开与关闭操作,代码如下。

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ToggleToolWidget extends LinearLayout implements OnClickListener {

    protected ImageView iv_flymodel = null;// 飞行模式
    View linearLayout = null;

    public ToggleToolWidget(Context context) {
        super(context);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View linearLayout = inflater.inflate(R.layout.toggle_tools, null);
        this.addView(linearLayout);
        ((Activity) getContext()).getWindow().getDecorView()
                .setBackgroundColor(Color.WHITE);
        /** 飞行模式 **/
        iv_flymodel = (ImageView) findViewById(R.id.flymodel);
        iv_flymodel.setOnClickListener(this);
        IntentFilter airPlaneIntentFilter = new IntentFilter(
                Intent.ACTION_AIRPLANE_MODE_CHANGED);
        getContext().registerReceiver(new AirPlaneBrocastReceiver(),
                airPlaneIntentFilter);
        changeAirPlaneImage();
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.flymodel) {
            // 取得目前飞行模式的状态
            boolean isAirPlaneOn;
            try {
                isAirPlaneOn = Settings.System
                        .getInt(getContext().getContentResolver(),
                                Settings.System.AIRPLANE_MODE_ON) == 1 ? true
                        : false;
                setAirplaneMode(!isAirPlaneOn);
            } catch (SettingNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    protected void changeAirPlaneImage() {
        try {
            boolean isAirPlaneOn = Settings.System.getInt(getContext()
                    .getContentResolver(), Settings.System.AIRPLANE_MODE_ON) == 1 ? true
                    : false;
            if (isAirPlaneOn) {
                iv_flymodel.setImageResource(R.drawable.fly_model_on);
            } else {
                iv_flymodel.setImageResource(R.drawable.fly_model_off);

            }
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @param setAirPlane
     */
    private void setAirplaneMode(boolean setAirPlane) {

        Settings.System.putInt(getContext().getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", setAirPlane);
        getContext().sendBroadcast(intent);
    }
    /**
     * 飞行模式广播接收器
     */
    class AirPlaneBrocastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            changeAirPlaneImage();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值