Safe-SettingActivity设置

设置的节目会涉及到很多功能,比如归属地的设置,显示的风格设置,黑名单设置,看门狗的设置等等设置,在AddressService中设置号码归属地显示的服务,LocationService提供了相关位置信息的服务手机防盗是通过广播进行实现的BootCompletedReceiver,首先设置一个安全号码进行保存,然后进行判断,比如当手机重新开机的时候会判断当前的号码是不是和安全号一致,同时向安全号码发送信息。

逻辑代码SettingActivity.java

package com.ldw.safe.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.ldw.safe.R;
import com.ldw.safe.service.AddressService;
import com.ldw.safe.service.CallSafeService;
import com.ldw.safe.service.WatchDogService;
import com.ldw.safe.utils.ServiceStatusUtils;
import com.ldw.safe.view.SettingClickView;
import com.ldw.safe.view.SettingItemView;

/**
 * 设置中心
 */
public class SettingActivity extends Activity {

	private SettingItemView siv_update;//设置自动更新
	private SettingItemView siv_address;//归属地
	private SettingClickView scv_address_style;//归属地显示的风格
	private SettingClickView scv_address_location;//初始化归属地的位置
	private SettingItemView siv_callsafe;//黑名单
	private SettingItemView siv_watch_dog;//看门狗
	
	private SharedPreferences mPref;//把设置的数据保存在mPref
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        
        //把设置的数据保存在mPref
        mPref = getSharedPreferences("config", MODE_PRIVATE);
               
        initUpdateView();//初始化自动升级开关
        initAdressView();//初始化归属地开关
        initAddressStyle();//初始化归属地样式的选择
        initAddressLocation();//初始化归属地出现的位置
        initBlackView();//黑名单设置开关
        initWatchDog();
	}
	
	/*
	 * 看门狗设置初始化
	 */
	private void initWatchDog() {
		siv_watch_dog = (SettingItemView) findViewById(R.id.siv_watch_dog);

        // 根据归属地服务是否运行来更新checkbox
        boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this,
                "com.ldw.safe.service.WatchDogService");

        if (serviceRunning) {
        	siv_watch_dog.setChecked(true);
        } else {
        	siv_watch_dog.setChecked(false);
        }

        siv_watch_dog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (siv_watch_dog.isChecked()) {
                	siv_watch_dog.setChecked(false);
                    stopService(new Intent(SettingActivity.this,
                    		WatchDogService.class));// 停止归属地服务
                } else {
                	siv_watch_dog.setChecked(true);
                    startService(new Intent(SettingActivity.this,
                    		WatchDogService.class));// 开启归属地服务
                }
            }
        });
		
	}
	
	/*
	 * 黑名单设置初始化
	 */
	private void initBlackView() {
		siv_callsafe = (SettingItemView) findViewById(R.id.siv_callsafe);

        // 根据归属地服务是否运行来更新checkbox
        boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this,
                "com.ldw.safe.service.CallSafeService");

        if (serviceRunning) {
            siv_callsafe.setChecked(true);
        } else {
            siv_callsafe.setChecked(false);
        }

        siv_callsafe.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (siv_callsafe.isChecked()) {
                    siv_callsafe.setChecked(false);
                    stopService(new Intent(SettingActivity.this,
                            CallSafeService.class));// 停止归属地服务
                } else {
                    siv_callsafe.setChecked(true);
                    startService(new Intent(SettingActivity.this,
                            CallSafeService.class));// 开启归属地服务
                }
            }
        });
		
	}

	/*
	 * 初始化自动升级开关
	 */
	private void initUpdateView(){
		siv_update = (SettingItemView)findViewById(R.id.siv_update);
        //siv_update.setTitle("自动更新设置");
        
        //获取保存的数据,判断之前选择的是开始还是关闭,初始化进入界面是否勾选
        boolean autoUpdate = mPref.getBoolean("auto_update", true);
        if(autoUpdate){
        	siv_update.setDesc("自动更新已经开启");
        	siv_update.setChecked(true);
        }else{
        	siv_update.setDesc("自动更新已经关闭");
        	siv_update.setChecked(false);
        }
        
        siv_update.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//判断右边框的勾选状态
				if(siv_update.isChecked()){
					//设置不勾选
					siv_update.setChecked(false);
					//siv_update.setDesc("自动更新已经关闭");
					//编辑mPref的值
					mPref.edit().putBoolean("auto_update", false).commit();
				}else{
					//设置勾选
					siv_update.setChecked(true);
					//siv_update.setDesc("自动更新已经开启");
					//编辑mPref的值
					mPref.edit().putBoolean("auto_update", true).commit();
				}
			}
        	
        });
	}
	
	/*
	 * 初始化归属地开关
	 */
	private void initAdressView(){
		
		siv_address = (SettingItemView) findViewById(R.id.siv_address);
		//判断归属地的服务是否在运行
		boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this, "com.ldw.safe.service.AddressService");
		//让服务的那个勾选框根据系统中是否有服务来判断是否去开启
		if(serviceRunning){
			siv_address.setChecked(true);
		}else{
			siv_address.setChecked(false);
		}
		
		siv_address.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(siv_address.isChecked()){
					siv_address.setChecked(false);
					stopService(new Intent(SettingActivity.this, AddressService.class));
				}else{
					siv_address.setChecked(true);
					//号码归属地显示开启的时候要开启服务
					startService(new Intent(SettingActivity.this, AddressService.class));
				}
				
			}
			
		});
	}
	
	/*
	 * 修改号码归属地提示框的风格
	 */
	private void initAddressStyle(){
		scv_address_style = (SettingClickView) findViewById(R.id.scv_address_style);
		
		scv_address_style.setTitle("归属地提示框风格");
		//获取保存的样式
		int style = mPref.getInt("address_Style", 0);
		//设置样式
		scv_address_style.setDesc(items[style]);
		
		scv_address_style.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSingleChooseDialog();
			}
			
		});
	}
	
	final String[] items = new String[]{"半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿"};
	
	/*
	 * 弹出归属地样式选择的单选框
	 */
	public void showSingleChooseDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//设置选择框额logo
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("归属地提示框风格");
		//获取保存的风格样式,默认为0
		int style = mPref.getInt("address_Style", 0);
		
		
		builder.setSingleChoiceItems(items, style, 
				new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				//保存选择的样式的序号
				mPref.edit().putInt("address_Style", which).commit();
				//点击以后对话框消失
				dialog.dismiss();
				//设置组合控件的文字描述
				scv_address_style.setDesc(items[which]);
				
			}
			
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}
	
	/*
	 * 初始化归属地出现的位置
	 */
	private void initAddressLocation(){
		scv_address_location = (SettingClickView) findViewById(R.id.scv_address_location);
		
		scv_address_location.setTitle("归属地提示框的显示位置");
		scv_address_location.setDesc("设置归属地提示框的显示位置");
		
		scv_address_location.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//点击以后直接跳转页面
				startActivity(new Intent(SettingActivity.this, DragViewActivity.class));
			}
			
		});
	}

}
AddressService.java号码归属地的显示,显示的时候是通过自定义的Toast,并且可以改变其位置

package com.ldw.safe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.db.dao.AddressDao;

/*
 * 来电提醒的服务
 */
public class AddressService extends Service {

	private TelephonyManager tm;
	private MyListener listener;
	private OutCallReceiver receiver;
	private WindowManager mWM;
	
	private int startX;
	private int startY;
	//private TextView view;
	private View view;
	private SharedPreferences mPref;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate(){
		super.onCreate();
		//读取样式的配置参数
		mPref = getSharedPreferences("config", MODE_PRIVATE);
		
		tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		listener = new MyListener();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);//监听打电话的状态
		
		receiver = new OutCallReceiver();
		IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
		registerReceiver(receiver, filter);//动态注册广播
	}
	
	class MyListener extends PhoneStateListener{
		//监听电话状态的变化
		@Override
		public void onCallStateChanged(int state, String incomingNumber){
			//
			switch(state){
			//电话铃声响起
			case TelephonyManager.CALL_STATE_RINGING:
				System.out.println("CALL_STATE_RINGING");
				//获取来电的号码归属地
				String address = AddressDao.getAddress(incomingNumber);
				//Toast的形式弹出号码归属地
				//Toast.makeText(AddressService.this, address, Toast.LENGTH_LONG).show();
				//自定义的Toast显示号码归属地
				showToast(address);
				break;
			//监听电话闲置的事件
			case TelephonyManager.CALL_STATE_IDLE:
				//关闭自定义的Toast显示
				if(mWM != null && view != null){
					//挂断电话的时候,移除窗体
					mWM.removeView(view);
				}
				break;
			default:
				break;
			}
			super.onCallStateChanged(state, incomingNumber);
		}
	}
	
	/*
	 * 监听去电 的广播接收者,添加权限PROCESS_OUTGOING_CALLS
	 */
	class OutCallReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			//获取到来电的号码
			String number = getResultData();
			//获取到号码的归属地
			String address = AddressDao.getAddress(number);
			//Toast的形式弹出号码归属地
			//Toast.makeText(context, address, Toast.LENGTH_LONG).show();
			//自定义的Toast显示号码归属地
			showToast(address);
			
		}
		
	}
	
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	//关闭服务,停止来电监听
    	tm.listen(listener, PhoneStateListener.LISTEN_NONE);
    	
    	//注销广播
    	unregisterReceiver(receiver);
    }
    
    /*
     * 自定义归属地显示浮窗,取代Toast,需要权限SYSTEM_ALERT_WINDOW
     */
    private void showToast(String text){
    	mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    	
    	//获取屏幕的宽度和高度
    	final int winWidth = mWM.getDefaultDisplay().getWidth();
    	final int winHeight = mWM.getDefaultDisplay().getHeight();
    	
    	final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
        final WindowManager.LayoutParams params = mParams;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        //将重心位置设置成左上方(0,0),默认实在重心位置
        params.gravity = Gravity.LEFT + Gravity.TOP;
        params.setTitle("Toast");
        
        //获取到之前归属地显示的坐标
        int lastX = mPref.getInt("lastX", 0);
        int lastY = mPref.getInt("lastY", 0);
        
        //params.x和 params.y是设置基于左上方的偏移量
        params.x = lastX;
        params.y = lastY;
        
        //原始的view显示
        //view = new TextView(this);
        
        //自定义的view显示
        view = View.inflate(this, R.layout.toast_address, null);
        
        //初始化样式
        int[] bgs = new int[] { R.drawable.call_locate_white,
				R.drawable.call_locate_orange, R.drawable.call_locate_blue,
				R.drawable.call_locate_gray, R.drawable.call_locate_green };
        //获取保存的样式数据
		int style = mPref.getInt("address_Style", 0);
		//设置样式
		view.setBackgroundResource(bgs[style]);
        
        TextView tvAddress= (TextView) view.findViewById(R.id.tv_address);
        tvAddress.setText(text);
        //tvAddress.setTextColor(Color.BLUE);
        //将view添加到窗体
        mWM.addView(view, params);
        
        //view响应点击事件
        view.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				System.out.println("onTouch...");
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					//获取起始点在屏幕中的坐标
					startX = (int) event.getRawX(); 
					startY = (int) event.getRawY(); 
					break;
				case MotionEvent.ACTION_MOVE:
					//获取结束点的坐标
					int endX = (int) event.getRawX();
					int endY = (int) event.getRawY();
					
					//计算偏移量
					int dx = endX - startX;
					int dy = endY - startY;
					//更新选民付出的位置
					params.x += dx;
					params.y += dy;
					//窗口x,y的最小最大的范围的限制,防止坐标越界
					if(params.x < 0){
						params.x = 0;
					}
					if(params.y < 0){
						params.y = 0;
					}
					if(params.x > winWidth - view.getWidth()){
						params.x =  winWidth - view.getWidth();
					}
					if(params.y > winHeight - view.getHeight()){
						params.y = winHeight - view.getHeight();
					}
					//窗口更新位置
					mWM.updateViewLayout(view, params);
					System.out.println("params.x" + params.x + "params.y" +params.y);
					
					// 重新初始化起点坐标
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					// 编辑保存记录坐标点
					Editor edit = mPref.edit();
					edit.putInt("lastX", params.x);
					edit.putInt("lastY", params.y);
					edit.commit();
					break;
				}
				return true;
			}
        	
        });
    }

}
LocationService.java这个Service提供了位置的相关信息,手机的位置信息,并把位置保存在SharePreference中

package com.ldw.safe.service;


import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;

/*
 * 获取经纬度坐标
 */
public class LocationService extends Service {

	private TextView tvLocation;
	private LocationManager lm;
	private MyLocationListener listener;
	public SharedPreferences mPref;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate(){
		super.onCreate();
		
		mPref = getSharedPreferences("config", MODE_PRIVATE);
		
		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        //获取所有的位置提供者(定位方法)
        //List<String> allProviders = lm.getAllProviders();
        //System.out.println(allProviders);
		//创建Criteria
		Criteria criteria = new Criteria();
		//是否允许付费
		criteria.setCostAllowed(true);
		criteria.setAccuracy(criteria.ACCURACY_FINE);
		//获取最佳的定位方法
        String bestProvider = lm.getBestProvider(criteria, true);
        
        listener = new MyLocationListener();
        //第一个参数是提供位置的方式,第二个参数是最短的更新位置的时间,第三个参数是最短的更新位置的距离
        //0表示随时更新
        lm.requestLocationUpdates(bestProvider, 0, 0, listener);
	}
	
	class MyLocationListener implements LocationListener{

    	//位置发生回掉的函数
		@Override
		public void onLocationChanged(Location location) {
			String latitude = "经度" + location.getLatitude();
			String longitude = "维度" + location.getLongitude();
			//保存获取的经纬度
			mPref.edit().putString("location", "latitude:" + latitude + "longitude:" +longitude).commit();
			
			
			//获取到经纬度就关闭掉服务,节约用电
			stopSelf();
			
		}

		//位置提供者状态发生变化的回掉
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}

		//用户把位置提供者打开的时候的回掉
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
    	
    }
    
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	//activity销毁时,关闭定位
    	lm.removeUpdates(listener);
    }

}

BootCompletedReceiver.java

package com.ldw.safe.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/*
 * 监听手机开机启动的广播
 */
public class BootCompletedReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		SharedPreferences mPref = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		
		//防盗保护开启的时候才做下面的处理
		boolean protect = mPref.getBoolean("protect", false);
		if(protect){
			
			String sim = mPref.getString("sim", null);//获取绑定的sim卡的信息
			
			if(!TextUtils.isEmpty(sim)){
	        	//获取当前的sim卡
				TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
				String currentSim = tm.getSimSerialNumber();
				//比较序列号是否一致
				if(sim.equals(currentSim)){
					System.out.println("手机安全");
				}else{
					System.out.println("sim卡变更了");
					//获取到保存的安全号码
					String phone = mPref.getString("safe_phone", null);
					SmsManager smsManager = SmsManager.getDefault();
					//向安全号码发送信息
					smsManager.sendTextMessage(phone, null, "sim card hs changerd", null, null);
				}
	        }
		}
		

		
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值