19、高级工具--设置中心的更新

来源:互联网 发布:打电话显示未注册网络 编辑:程序博客网 时间:2024/04/28 19:18

由于添加了高级工具的“来电显示归属地”的功能,需要对设置中心进行更新,实现的界面效果:


对应的布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView style="@style/title_center_text"        android:text="设置中心" />    <View style="@style/splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_autoupdate">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="自动更新设置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text="自动更新已开启"                android:id="@+id/tv_setting_autoupdate_status"/>        </LinearLayout>        <CheckBox style="@style/right_checkbox"            android:id="@+id/cb_setting_autoupdate"            android:checked="true" />    </RelativeLayout>    <View style="@style/dot_splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_show_location">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="来电归属地设置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text="来电归属地显示没有开启"                android:id="@+id/tv_setting_show_location_status"/>        </LinearLayout>        <CheckBox style="@style/right_checkbox"            android:id="@+id/cb_setting_showlocation"            android:checked="true" />    </RelativeLayout>    <View style="@style/dot_splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_showlocation_style">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="来电归属地风格设置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text=""                android:id="@+id/tv_setting_showlocation_style"/>        </LinearLayout>        <ImageView style="@style/right_arrow_image"/>    </RelativeLayout>    <View style="@style/dot_splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_show_location_pos">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="归属地提示框的位置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text="更改归属地提示框的位置"/>        </LinearLayout>        <ImageView style="@style/right_arrow_image"/>    </RelativeLayout>    <View style="@style/dot_splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_black_call">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="来电黑名单设置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text="来电黑名单拦截没有开启"                android:id="@+id/tv_setting_blackcall_status"/>        </LinearLayout>        <CheckBox style="@style/right_checkbox"            android:id="@+id/cb_setting_blackcall"            android:checked="true" />    </RelativeLayout>    <View style="@style/dot_splitter_view"/>    <RelativeLayout style="@style/relativelayout" android:id="@+id/rl_setting_applocker">        <LinearLayout style="@style/vertical_linearlayout"            android:layout_alignParentLeft="true">            <TextView style="@style/content_text"                android:text="程序锁设置"                android:layout_gravity="center_horizontal|left"                android:layout_alignParentTop="true"                android:layout_alignParentLeft="true" />            <TextView style="@style/commontext"                android:text="程序锁服务没有开启"                android:id="@+id/tv_setting_applocker_status"/>        </LinearLayout>        <CheckBox style="@style/right_checkbox"            android:id="@+id/cb_setting_applocker"            android:checked="true" />    </RelativeLayout>    <View style="@style/dot_splitter_view"/></LinearLayout>
这里大量使用了style:

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="horizontal_linearlayout">        <!--该样式用于水平布局-->        <item name="android:orientation">horizontal</item>        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">wrap_content</item>    </style>    <style name="vertical_linearlayout">        <!--该样式用于垂直布局-->        <item name="android:orientation">vertical</item>        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>    <style name="relativelayout">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">wrap_content</item>    </style>    <style name="splitter_view">        <!--该样式用于分割条-->        <item name="android:layout_width">fill_parent</item>        <item name="android:layout_height">1dp</item>        <item name="android:background">@drawable/devide_line</item>        <item name="android:layout_marginTop">5dp</item>    </style>    <style name="dot_splitter_view" parent="splitter_view">        <!--该样式用于分割条-->        <item name="android:background">@drawable/listview_devider</item>    </style>    <style name="title_text">        <!--该样式用于标题-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_gravity">fill_horizontal</item>        <item name="android:textSize">30sp</item>        <item name="android:textStyle">bold</item>    </style>    <style name="title_center_text" parent="title_text">        <!--该样式用于居中标题-->        <item name="android:layout_gravity">center_horizontal</item>    </style>    <style name="content_text">        <!--该样式用于内容文本-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textSize">20sp</item>    </style>    <style name="content_withimage_text" parent="content_text">        <!--该样式用于前面有image控件的内容文本-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_gravity">right</item>    </style>    <style name="commontext">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>    <style name="text_start_image">        <!--该样式用于文本前的图像-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_gravity">left</item>        <item name="android:src">@drawable/text_start_image</item>    </style>    <style name="right_arrow_image">        <!--该样式用于向右箭头的图像-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_alignParentRight">true</item>        <item name="android:src">@drawable/jiantou1</item>    </style>    <style name="dot_disable_image">        <!--灰化圆圈图像-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:src">@drawable/dot_disable</item>    </style>    <style name="dot_enable_image">        <!--绿色圆圈图像-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:src">@drawable/dot_enable</item>    </style>    <style name="center_icon_image">        <item name="android:layout_width">fill_parent</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:gravity">center_horizontal</item>        <item name="android:layout_marginTop">40dp</item>        <item name="android:layout_marginBottom">40dp</item>    </style>    <style name="prev_button">        <!--上一步-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_gravity">left</item>        <item name="android:layout_alignParentLeft">true</item>        <item name="android:text">上一步</item>        <item name="android:drawableLeft">@drawable/prev</item>        <item name="android:onClick">prev</item>    </style>    <style name="next_button">        <!--下一步-->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_gravity">right</item>        <item name="android:layout_alignParentRight">true</item>        <item name="android:text">下一步</item>        <item name="android:drawableRight">@drawable/next</item>        <item name="android:onClick">next</item>    </style>    <style name="right_checkbox">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:singleLine">false</item>        <item name="android:textSize">30sp</item>        <item name="android:layout_alignParentRight">true</item>    </style></resources>

设置中心代码实现:

package com.example.mobilesafe;import android.app.Activity;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 {    //设置自动更新的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 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);                }            }        });    }    @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();    }}
设置中心的“来电显示是否开启”是靠判断服务是否运行来判定的,具体的代码:

package com.example.utils;import android.app.ActivityManager;import android.content.Context;import java.util.List;/** * Created by sing on 14-1-13. * desc:判断某个服务是否处于运行状态 */public class ServiceStatusUtil {    public static boolean isServiceRunning(Context context, String serviceName) {        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningServiceInfo> infos = am.getRunningServices(100);        for (ActivityManager.RunningServiceInfo info : infos) {            if (info.service.getClassName().equals(serviceName)) {                return true;            }        }        return false;    }}






0 0
原创粉丝点击