20、高级工具--更改归属地显示风格

点击设置中心的“来电归属地风格设置”弹出如下对话框供用户选择

设置中心代码:

package com.example.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.mobilesafe.service.ShowCallLocationService;
import com.example.utils.ServiceStatusUtil;

/**
 * Created by sing on 13-12-24.
 * desc:
 */
public class SettingCenterActivity extends Activity {

    //背景风格
    private static final String[] bg_styles = {"半透明", "活力橙", "卫士蓝", "苹果绿", "金属灰"};

    //设置自动更新的checkbox
    private View rl_setting_autoupdate;
    private CheckBox cb_setting_autoupdate;

    //显示自动更新开启状态的文本框
    private TextView tv_setting_autoupdate_status;

    private View rl_setting_show_location;
    private CheckBox cb_setting_showlocation;
    private TextView tv_setting_show_location_status;

    //来电归属地显示风格
    private View rl_setting_showlocation_style;
    private TextView tv_setting_showlocation_style;

    //开启来电归属地信息显示的意图
    private Intent showLocationIntent;

    //保存配置
    private SharedPreferences sp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingcenter);

        tv_setting_autoupdate_status = (TextView) findViewById(R.id.tv_setting_autoupdate_status);
        cb_setting_autoupdate = (CheckBox) findViewById(R.id.cb_setting_autoupdate);

        //加载上次设置的状态
        sp = getSharedPreferences("config", MODE_PRIVATE);
        boolean autoupdate = sp.getBoolean("autoupdate", true);
        cb_setting_autoupdate.setChecked(autoupdate);
        tv_setting_autoupdate_status.setText(autoupdate ? "自动更新已经开启" : "自动更新已经关闭");
        tv_setting_autoupdate_status.setTextColor(autoupdate ? Color.WHITE : Color.GRAY);

        //为checkbox编写响应事件
        rl_setting_autoupdate = findViewById(R.id.rl_setting_autoupdate);
        rl_setting_autoupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean b = !cb_setting_autoupdate.isChecked();
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("autoupdate", b);
                editor.commit();
                cb_setting_autoupdate.setChecked(b);
                tv_setting_autoupdate_status.setText(b ? "自动更新已经开启" : "自动更新已经关闭");
                tv_setting_autoupdate_status.setTextColor(b ? Color.WHITE : Color.GRAY);
            }
        });

        //来电归属地设置
        showLocationIntent = new Intent(this, ShowCallLocationService.class);
        tv_setting_show_location_status = (TextView)findViewById(R.id.tv_setting_show_location_status);
        cb_setting_showlocation = (CheckBox) findViewById(R.id.cb_setting_showlocation);
        rl_setting_show_location = findViewById(R.id.rl_setting_show_location);
        rl_setting_show_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cb_setting_showlocation.isChecked()) {
                    cb_setting_showlocation.setChecked(false);
                    tv_setting_show_location_status.setText("来电归属地显示没有开启");
                    tv_setting_show_location_status.setTextColor(Color.GRAY);
                    //开启来电归属地显示
                    stopService(showLocationIntent);
                }else{
                    cb_setting_showlocation.setChecked(true);
                    tv_setting_show_location_status.setText("来电归属地显示已经开启");
                    tv_setting_show_location_status.setTextColor(Color.WHITE);
                    //关闭来电归属地显示
                    startService(showLocationIntent);
                }
            }
        });

        //来电归属地风格设置
        tv_setting_showlocation_style = (TextView) findViewById(R.id.tv_setting_showlocation_style);
        int style = sp.getInt("which", 0);
        tv_setting_showlocation_style.setText(bg_styles[style]);
        rl_setting_showlocation_style = findViewById(R.id.rl_setting_showlocation_style);
        rl_setting_showlocation_style.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChooseBgDlg();
            }
        });

    }

    @Override
    protected void onResume() {
        boolean serviceRunning = ServiceStatusUtil.isServiceRunning(this, "com.example.mobilesafe.service.ShowCallLocationService");
        cb_setting_showlocation.setChecked(serviceRunning);
        tv_setting_show_location_status.setText(serviceRunning ? "来电归属地显示已经开启" : "来电归属地显示没有开启");
        tv_setting_show_location_status.setTextColor(serviceRunning ? Color.WHITE : Color.GRAY);

        super.onResume();
    }

    //选择背景颜色对话框
    private void showChooseBgDlg() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.notification);
        builder.setTitle("归属地提示框风格");
        int which = sp.getInt("which", 0);
        builder.setSingleChoiceItems(bg_styles,which,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("which", i);
                editor.commit();
                tv_setting_showlocation_style.setText(bg_styles[i]);
                dialogInterface.dismiss();
            }
        });

        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

        builder.create().show();
    }
}
主要功能在showChooseBgDlg中。

另外在服务中使用设置的背景:

package com.example.mobilesafe.service;

import android.app.ActionBar;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.db.NumberAddressDao;

/**
 * Created by sing on 14-1-13.
 * desc:监听电话呼入的服务
 */
public class ShowCallLocationService extends Service {

    //"半透明", "活力橙", "卫士蓝", "苹果绿", "金属灰"
    public static final int[] bg_styles = {R.drawable.call_locate_white,
            R.drawable.call_locate_orange, R.drawable.call_locate_blue,
            R.drawable.call_locate_green, R.drawable.call_locate_gray};

    //电话管理器
    private TelephonyManager tm;

    //窗体管理器
    private WindowManager windowManager;

    //电话状态改变的监听器
    private MyPhoneListener listener;

    //保存配置
    private SharedPreferences sp;

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        sp = getSharedPreferences("config", MODE_PRIVATE);
        listener = new MyPhoneListener();
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    }

    private class MyPhoneListener extends PhoneStateListener {

        private View view;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                //电话铃声正在响
                case TelephonyManager.CALL_STATE_RINGING:
                    //查询出电话号码的归属地
                    NumberAddressDao numberAddressDao = new NumberAddressDao(getApplicationContext());
                    String address = numberAddressDao.getAddress(incomingNumber);

                    //通过布局填充器将布局转换为view
                    view = View.inflate(getApplicationContext(), R.layout.show_address, null);
                    LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_show_address);
                    int style = sp.getInt("which", 0);
                    ll.setBackgroundResource(bg_styles[style]);
                    TextView tv = (TextView) view.findViewById(R.id.tv_show_address);
                    tv.setText(address);

                    final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    //显示在窗体上的style为半透明
                    params.format = PixelFormat.TRANSLUCENT;
                    //窗体view的类型为吐司
                    params.type = WindowManager.LayoutParams.TYPE_TOAST;
                    windowManager.addView(view, params);
                    break;
                //电话空闲状态
                case TelephonyManager.CALL_STATE_IDLE:
                    //将窗体上的吐司移除
                    if (view != null) {
                        windowManager.removeView(view);
                        view = null;
                    }
                    break;
                //电话接通状态
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //将窗体上的吐司移除
                    if (view != null) {
                        windowManager.removeView(view);
                        view = null;
                    }
                    break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }

    }
}
主要代码是:

                    LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_show_address);
                    int style = sp.getInt("which", 0);
                    ll.setBackgroundResource(bg_styles[style]);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值