你的位置:首页 > Java教程

[Java教程]Android 手机卫士6


1.包含内容

高级工具:归属地查询;常用号码查询;短信备份;

2.归属地查询

①拷贝数据库

SQLiteDatabase不支持直接从assets读取文件,所以要提前拷贝数据

②电话归属地在手机窗体上悬浮显示

电话状态的监听(服务开启的时候,需要去做监听,
    mTM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    mPhoneStateListener = new MyPhoneStateListener();
    mTM.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
关闭的时候电话状态就不需要监听)
  if(mTM!=null && mPhoneStateListener!=null){
    mTM.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
  }

③来电监听 :创建后台服务 AddressService

public void onCreate() {  listener = new MyPhoneListener();  tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);  tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);};@Overridepublic void onDestroy() {  super.onDestroy();  tm.listen(listener, PhoneStateListener.LISTEN_NONE);  listener = null;}class MyPhoneListener extends PhoneStateListener {  @Override  public void onCallStateChanged(int state, String incomingNumber) {    switch (state) {    case TelephonyManager.CALL_STATE_RINGING:      String address = NumberAddressDao.getAddress(incomingNumber);      Toast.makeText(AddressService.this, address, Toast.LENGTH_LONG)          .show();      break;    default:      break;    }    super.onCallStateChanged(state, incomingNumber);  }}

 ④去电监听

动态注册广播

当启动后台服务时,注册广播,服务停止后,注销广播,这样的话,来电和去电的位置显示都可以由一个开关来控制

 

⑤电话号码归属的方格,可以跟随手势去做移动

1.在设置界面添加一个可点击条目,点击此条目弹出activity(半透明)
2.双击居中的view,和描述文字处在不同的竖直(上下)区域
3.限制view的可拖拽范围
4.view双击居中

ToastLocationActivity.java

 1 package com.itheima.mobilesafe74.activity; 2  3 import com.itheima.mobilesafe74.R; 4 import com.itheima.mobilesafe74.utils.ConstantValue; 5 import com.itheima.mobilesafe74.utils.SpUtil; 6  7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.view.MotionEvent; 10 import android.view.View; 11 import android.view.View.OnTouchListener; 12 import android.view.WindowManager; 13 import android.widget.Button; 14 import android.widget.ImageView; 15 import android.widget.RelativeLayout; 16 import android.widget.RelativeLayout.LayoutParams; 17  18 public class ToastLocationActivity extends Activity { 19   private ImageView iv_drag; 20   private Button bt_top,bt_bottom; 21   private WindowManager mWM; 22   private int mScreenHeight; 23   private int mScreenWidth; 24    25  26   @Override 27   protected void onCreate(Bundle savedInstanceState) { 28     super.onCreate(savedInstanceState); 29     setContentView(R.layout.activity_toast_location); 30      31     initUI(); 32   } 33  34   private void initUI() { 35     //可拖拽双击居中的图片控件 36     iv_drag = (ImageView) findViewById(R.id.iv_drag); 37     bt_top = (Button) findViewById(R.id.bt_top); 38     bt_bottom = (Button) findViewById(R.id.bt_bottom); 39      40     mWM = (WindowManager) getSystemService(WINDOW_SERVICE); 41     mScreenHeight = mWM.getDefaultDisplay().getHeight(); 42     mScreenWidth = mWM.getDefaultDisplay().getWidth(); 43      44     int locationX = SpUtil.getInt(getApplicationContext(), ConstantValue.LOCATION_X, 0); 45     int locationY = SpUtil.getInt(getApplicationContext(), ConstantValue.LOCATION_Y, 0); 46      47     //左上角坐标作用在iv_drag上 48     //iv_drag在相对布局中,所以其所在位置的规则需要由相对布局提供 49      50     //指定宽高都为WRAP_CONTENT 51     LayoutParams layoutParams = new RelativeLayout.LayoutParams( 52         RelativeLayout.LayoutParams.WRAP_CONTENT,  53         RelativeLayout.LayoutParams.WRAP_CONTENT); 54     //将左上角的坐标作用在iv_drag对应规则参数上 55     layoutParams.leftMargin = locationX; 56     layoutParams.topMargin = locationY; 57     //将以上规则作用在iv_drag上 58     iv_drag.setLayoutParams(layoutParams); 59      60     if(locationY>mScreenHeight/2){ 61       bt_bottom.setVisibility(View.INVISIBLE); 62       bt_top.setVisibility(View.VISIBLE); 63     }else{ 64       bt_bottom.setVisibility(View.VISIBLE); 65       bt_top.setVisibility(View.INVISIBLE); 66     } 67      68      69      70     //监听某一个控件的拖拽过程(按下(1),移动(多次),抬起(1)) 71     iv_drag.setOnTouchListener(new OnTouchListener() { 72       private int startX; 73       private int startY; 74       //对不同的事件做不同的逻辑处理 75       @Override 76       public boolean onTouch(View v, MotionEvent event) { 77         switch (event.getAction()) { 78         case MotionEvent.ACTION_DOWN: 79           startX = (int) event.getRawX(); 80           startY = (int) event.getRawY(); 81           break; 82         case MotionEvent.ACTION_MOVE: 83           int moveX = (int) event.getRawX(); 84           int moveY = (int) event.getRawY(); 85            86           int disX = moveX-startX; 87           int disY = moveY-startY; 88            89           //1,当前控件所在屏幕的(左,上)角的位置 90           int left = iv_drag.getLeft()+disX;//左侧坐标 91           int top = iv_drag.getTop()+disY;//顶端坐标 92           int right = iv_drag.getRight()+disX;//右侧坐标 93           int bottom = iv_drag.getBottom()+disY;//底部坐标 94            95           //容错处理(iv_drag不能拖拽出手机屏幕) 96           //左边缘不能超出屏幕 97           if(left<0){ 98             return true; 99           }100           101           //右边边缘不能超出屏幕102           if(right>mScreenWidth){103             return true;104           }105           106           //上边缘不能超出屏幕可现实区域107           if(top<0){108             return true;109           }110           111           //下边缘(屏幕的高度-22 = 底边缘显示最大值)112           if(bottom>mScreenHeight - 22){113             return true;114           }115           116           if(top>mScreenHeight/2){117             bt_bottom.setVisibility(View.INVISIBLE);118             bt_top.setVisibility(View.VISIBLE);119           }else{120             bt_bottom.setVisibility(View.VISIBLE);121             bt_top.setVisibility(View.INVISIBLE);122           }123           124           //2,告知移动的控件,按计算出来的坐标去做展示125           iv_drag.layout(left, top, right, bottom);126           127           //3,重置一次其实坐标128           startX = (int) event.getRawX();129           startY = (int) event.getRawY();130           131           break;132         case MotionEvent.ACTION_UP:133           //4,存储移动到的位置134           SpUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_X, iv_drag.getLeft());135           SpUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_Y, iv_drag.getTop());136           break;137         }138         //在当前的情况下返回false不响应事件,返回true才会响应事件139         return true;140       }141     });142   }143 }

ToastLocationActivity

AndroidManifest.

<activity android:name="com.itheima.mobilesafe74.activity.ToastLocationActivity"       android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

activity_toast_location.

 1 <? 2 <RelativeLayout  3   android:layout_width="match_parent" 4   android:layout_height="match_parent"  5   android:background="#accc"> 6   <!-- imageView在相对布局中,所以其所在位置的规则需要由相对布局提供 --> 7   <ImageView  8     android:id="@+id/iv_drag" 9     android:background="@drawable/drag"10     android:layout_width="wrap_content"11     android:layout_height="wrap_content"/>12   <Button 13     android:id="@+id/bt_top"14     android:layout_width="match_parent"15     android:text="按中提示框拖拽到任意位置"16     android:gravity="center"17     android:visibility="invisible"18     android:background="@drawable/call_locate_blue"19     android:layout_height="wrap_content"/>20   <Button 21     android:layout_alignParentBottom="true"22     android:id="@+id/bt_bottom"23     android:layout_width="match_parent"24     android:text="按中提示框拖拽到任意位置"25     android:gravity="center"26     android:visibility="visible"27     android:background="@drawable/call_locate_blue"28     android:layout_height="wrap_content"/>29 </RelativeLayout>

View Code