手机安全卫士——手机防盗页面

LostFindActivity.java

public class LostFindActivity extends Activity {
    private SharedPreferences mPrefs;

    private TextView tvSafePhone;
    private ImageView ivProtect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPrefs = getSharedPreferences("config", MODE_PRIVATE);

        boolean configed = mPrefs.getBoolean("configed", false);// 判断是否进入过设置向导
        if (configed) {
            setContentView(R.layout.activity_lost_find);

            // 根据sp更新安全号码
            tvSafePhone = (TextView) findViewById(R.id.tv_safe_phone);
            String phone = mPrefs.getString("safe_phone", "");
            tvSafePhone.setText(phone);

            // 根据sp更新保护锁
            ivProtect = (ImageView) findViewById(R.id.iv_protect);
            boolean protect = mPrefs.getBoolean("protect", false);
            if (protect) {
                ivProtect.setImageResource(R.drawable.lock);
            } else {
                ivProtect.setImageResource(R.drawable.unlock);
            }

        } else {
            // 跳转设置向导页
            startActivity(new Intent(this, Setup1Activity.class));
            finish();
        }
    }

    /**
     * 重新进入设置向导
     * 
     * @param view
     */
    public void reEnter(View view) {
        startActivity(new Intent(this, Setup1Activity.class));
        finish();
    }
}
activity_lost_find.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        style="@style/TitleStyle"
        android:text="手机防盗" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center_vertical" >

        <TextView
            style="@style/ContentStyle"
            android:text="安全号码" />

        <TextView
            android:id="@+id/tv_safe_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            
            android:textColor="@color/black"
            android:textSize="16sp" />
    </RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/listview_divider" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center_vertical" >

        <TextView
            style="@style/ContentStyle"
            android:text="防盗保护是否开启" />

        <ImageView
            android:id="@+id/iv_protect"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/unlock" />
    </RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/listview_divider" />

    <TextView
        android:layout_width="match_parent"
        android:background="@drawable/shape_selector"
        android:onClick="reEnter"
        android:clickable="true"
        style="@style/ContentStyle"
        android:layout_margin="5dp"
        android:text="重新进入设置向导" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/listview_divider" />
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="功能简介"
        android:layout_margin="5dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:background="#5000"
        />
    
    <TextView 
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center"
        android:text="GPS追踪:#*location*#"
        />
    <TextView 
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center"
        android:text="播放报警音乐:#*alarm*#"
        />
    <TextView 
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center"
        android:text="远程删除数据:#*wipedata*#"
        />
    <TextView 
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center"
        android:text="远程锁屏:#*lockscreen*#"
        />
    

</LinearLayout>
shape_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/gradient_box" android:state_pressed="true"></item>
    
    <item android:drawable="@android:color/transparent"></item><!--默认  -->

</selector>
gradient_box.xml   
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >//矩形
    <!-- 渐变颜色 -->
    <gradient
        android:angle="45"
        android:centerColor="#fff"
        android:endColor="#f00"
        android:startColor="#0f0" >
    </gradient>

    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" >
    </padding>
    <!-- 角度 -->
    <corners android:radius="5dp" />

</shape>

 

下边是设置向导:

 

BaseSetupActivity.java

//不需要在清单文件中注册,因为不需要界面展示
public
abstract class BaseSetupActivity extends Activity { private GestureDetector mDectector; public SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPref = getSharedPreferences("config", MODE_PRIVATE); // 手势识别器 mDectector = new GestureDetector(this, new SimpleOnGestureListener() { /** * 监听手势滑动事件 e1表示滑动的起点,e2表示滑动终点 velocityX表示水平速度 velocityY表示垂直速度 */ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 判断纵向滑动幅度是否过大, 过大的话不允许切换界面 if (Math.abs(e2.getRawY() - e1.getRawY()) > 100) { Toast.makeText(BaseSetupActivity.this, "不能这样划哦!", Toast.LENGTH_SHORT).show(); return true; } // 判断滑动是否过慢 if (Math.abs(velocityX) < 100) { Toast.makeText(BaseSetupActivity.this, "滑动的太慢了!", Toast.LENGTH_SHORT).show(); return true; } // 向右划,上一页 if (e2.getRawX() - e1.getRawX() > 200) { showPreviousPage(); return true; } // 向左划, 下一页 if (e1.getRawX() - e2.getRawX() > 200) { showNextPage(); return true; } return super.onFling(e1, e2, velocityX, velocityY); } }); } /** * 展示下一页, 子类必须实现 */ public abstract void showNextPage(); /** * 展示上一页, 子类必须实现 */ public abstract void showPreviousPage();//手势识别和点击按钮都会进入这里 // 点击下一页按钮 public void next(View view) { showNextPage(); } // 点击上一页按钮 public void previous(View view) { showPreviousPage(); } @Override public boolean onTouchEvent(MotionEvent event) { mDectector.onTouchEvent(event);// 委托手势识别器处理触摸事件 return super.onTouchEvent(event); } }

Setup1Activity.java

public class Setup1Activity extends BaseSetupActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup1);
    }

    @Override
    public void showNextPage() {
        startActivity(new Intent(this, Setup2Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画
    }

    @Override
    public void showPreviousPage() {

    }
}
activity_setup1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >

    <TextView
        style="@style/TitleStyle"
        android:text="1.欢迎使用手机防盗" />

    <TextView
        style="@style/ContentStyle"
        android:text="您的手机防盗卫士:" />

    <TextView
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/btn_star"
        android:gravity="center"
        android:text="SIM卡变更报警" />

    <TextView
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/btn_star"
        android:gravity="center"
        android:text="GPS追踪" />

    <TextView
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/btn_star"
        android:gravity="center"
        android:text="远程销毁数据" />

    <TextView
        style="@style/ContentStyle"
        android:drawableLeft="@android:drawable/btn_star"
        android:gravity="center"
        android:text="远程锁屏" />
    <LinearLayout      //这是那四个 小点
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            />
         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />    
    </LinearLayout> 
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <ImageView 
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/setup1"/>
        <Button 
            style="@style/NextStyle"
            />
    </RelativeLayout>
</LinearLayout>

styles.xml

<resources>
    <style name="ContentStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        
        <item name="android:textColor">@color/black</item>
        
        <item name="android:textSize">18sp</item>
        <item name="android:layout_margin">10dp</item>
    </style>
    
     <style name="NextStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:layout_alignParentRight">true</item>
        <item name="android:background">@drawable/btn_green_selector</item>
        <item name="android:drawableRight">@drawable/next</item>
        <item name="android:text">下一步</item>
        <item name = "android:onClick">next</item>
    </style>
    <style name="PreviousStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:layout_alignParentLeft">true</item>
        <item name="android:background">@drawable/btn_green_selector</item>  //将状态选择器设置给button
        <item name="android:drawableRight">@drawable/previous</item>
        <item name="android:text">上一步</item>
        <item name = "android:onClick">previous</item>
    </style>
</resources>

btn_green_selector.xml   //状态选择器(selector)设置给view,点击会有相应的效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"   >
    <item android:drawable="@drawable/function_greenbutton_pressed" //.9.png
          android:state_pressed="true" //按下
        ></item>
    <item android:drawable="@drawable/function_greenbutton_pressed"//.9.png
          android:state_focused="true" //获取焦点
        ></item>
    <item android:drawable="@drawable/btn_green_normal"></item> //默认
</selector>

 

.9.png(9patch)图片的制作:

通过黑色边线来描述图片的拉伸情况和填充文字的方式

上边线-水平拉伸;左边线-垂直拉伸;右边线-垂直填充区域;下边线-水平填充区域

   

    

 

Setup2Activity.java

public class Setup2Activity extends BaseSetupActivity {
    
    private SettingItemView sivSim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup2);

        sivSim = (SettingItemView) findViewById(R.id.siv_sim);

        String sim = mPref.getString("sim", null);
        if (!TextUtils.isEmpty(sim)) {
            sivSim.setChecked(true);
        } else {
            sivSim.setChecked(false);
        }

        sivSim.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (sivSim.isChecked()) {
                    sivSim.setChecked(false);
                    mPref.edit().remove("sim").commit();// 删除已绑定的sim卡
                } else {
                    sivSim.setChecked(true);
                    // 选中的话,保存sim卡信息
                    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    String simSerialNumber = tm.getSimSerialNumber();// 获取sim卡序列号
                    System.out.println("sim卡序列号:" + simSerialNumber);

                    mPref.edit().putString("sim", simSerialNumber).commit();// 将sim卡序列号保存在sp中
                }
            }
        });
    }

    @Override
    public void showNextPage() {
        // 如果sim卡没有绑定,就不允许进入下一个页面
        String sim = mPref.getString("sim", null);
        if (TextUtils.isEmpty(sim)) {
            ToastUtils.showToast(this, "必须绑定sim卡!");
            return;
        }

        startActivity(new Intent(this, Setup3Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画
    }

    @Override
    public void showPreviousPage() {
        startActivity(new Intent(this, Setup1Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_previous_in,
                R.anim.trans_previous_out);// 进入动画和退出动画
    }
}

BootCompleteReceiver.java

public class BootCompleteReceiver extends BroadcastReceiver {
    // 监听手机开机启动的广播    
        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferences sp = context.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            boolean protect = sp.getBoolean("protect", false);
            // 只有在防盗保护开启的前提下才进行sim卡判断
            if (protect) {
                String sim = sp.getString("sim", null);// 获取绑定的sim卡

                if (!TextUtils.isEmpty(sim)) {
                    // 获取当前手机的sim卡
                    TelephonyManager tm = (TelephonyManager) context
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    String currentSim = tm.getSimSerialNumber() + "111";// 拿到当前手机的sim卡

                    if (sim.equals(currentSim)) {
                        System.out.println("手机安全");
                    } else {
                        System.out.println("sim卡已经变化, 发送报警短信!!!");
                        String phone = sp.getString("safe_phone", "");// 读取安全号码

                        // 发送短信给安全号码
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(phone, null,
                                "sim card changed!", null, null);
                    }
                }
            }
        }

}

 

我们开启5554和5556的模拟器,给5554安全号码设置为5556,我们给5554的sim卡序列号变化,那么开机重启5554,开机启动的广播就会
检测到sim卡变更,发送短信给5556,5556就会接收到5554发送来的短信。
SmsReceiver .java
//拦截短信
public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
//pdu为承载着一条短信的所有短信。一条短信为140个英文字符长度,在这个长度范围内,即需一个pdu即可。超出这个范围,即要分割成多个pdu数组。 Object[] objects
= (Object[]) intent.getExtras().get("pdus"); for (Object object : objects) {// 短信最多140字节, // 超出的话,会分为多条短信发送,所以是一个数组,因为我们的短信指令很短,所以for循环只执行一次 SmsMessage message = SmsMessage.createFromPdu((byte[]) object); String originatingAddress = message.getOriginatingAddress();// 短信来源号码 String messageBody = message.getMessageBody();// 短信内容 System.out.println(originatingAddress + ":" + messageBody); if ("#*alarm*#".equals(messageBody)) { // 播放报警音乐, 即使手机调为静音,也能播放音乐, 因为使用的是媒体声音的通道,和铃声无关 MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs); player.setVolume(1f, 1f); player.setLooping(true); player.start(); abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了 } else if ("#*location*#".equals(messageBody)) { // 获取经纬度坐标 context.startService(new Intent(context, LocationService.class));// 开启定位服务 SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); String location = sp.getString("location", "getting location..."); System.out.println("location:" + location); abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了 } else if ("#*wipedata*#".equals(messageBody)) { System.out.println("远程清除数据"); abortBroadcast(); } else if ("#*lockscreen*#".equals(messageBody)) { System.out.println("远程锁屏"); abortBroadcast(); } } } }

 我们在5556的手机上发送短信,5554的手机就会播放R.raw.ylzs的音乐。
LocationService.java

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

    private LocationManager lm;
    private MyLocationListener listener;
    private SharedPreferences mPref;

    @Override
    public IBinder onBind(Intent intent) {
        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 = new Criteria();
        criteria.setCostAllowed(true);// 是否允许付费,比如使用3g网络定位
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String bestProvider = lm.getBestProvider(criteria, true);// 获取最佳位置提供者

        listener = new MyLocationListener();
        lm.requestLocationUpdates(bestProvider, 0, 0, listener);// 参1表示位置提供者,参2表示最短更新时间,参3表示最短更新距离
    }

    class MyLocationListener implements LocationListener {

        // 位置发生变化
        @Override
        public void onLocationChanged(Location location) {
            System.out.println("get location!");

            // 将获取的经纬度保存在sp中
            mPref.edit().putString("location", "j:" + location.getLongitude() + "; w:" + location.getLatitude())
                    .commit();

            stopSelf();// 停掉service,这样节电
        }

        // 位置提供者状态发生变化
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            System.out.println("onStatusChanged");
        }

        // 用户打开gps
        @Override
        public void onProviderEnabled(String provider) {
            System.out.println("onProviderEnabled");
        }

        // 用户关闭gps
        @Override
        public void onProviderDisabled(String provider) {
            System.out.println("onProviderDisabled");
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lm.removeUpdates(listener);// 当activity销毁时,停止更新位置, 节省电量
    }

}
 
trans_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%p"
    android:toXDelta="0" >
 <!-- p是parent -->
</translate>
trans_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="500"
   android:fromXDelta="0"
   android:toXDelta="-100%p"  
    >
</translate>
trans_previous_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%p"
    android:toXDelta="0"     
    >
</translate>

trans_previous_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0"
    android:toXDelta="100%p" >

</translate>
activity_setup2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mobilesafe="http://schemas.android.com/apk/res/com.mxn.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/TitleStyle"
        android:text="2.手机卡绑定"
        
        />
    <TextView 
        style="@style/ContentStyle"
        android:text="通过绑定SIM卡:"
        />
      <TextView 
        style="@style/ContentStyle"
        android:text="下次重启手机如果发现SIM卡变化"
        />
      <TextView 
        style="@style/ContentStyle"
        android:text="就会发送报警短信"
        />
       <com.mxn.mobilesafe.view.SettingItemView
        android:id="@+id/siv_sim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mobilesafe:title="点击绑定SIM卡"
        mobilesafe:desc_on="SIM卡已经绑定"
        mobilesafe:desc_off="SIM卡没有绑定" >
    </com.mxn.mobilesafe.view.SettingItemView>
    
     <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            />
          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
        
        
    </LinearLayout>
       
       
      <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <ImageView 
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/setup2"/>
        <Button 
            style="@style/NextStyle"
            />
        <Button 
            style="@style/PreviousStyle"/>
         
            
    </RelativeLayout>

</LinearLayout>

 Setup3Activity.java

public class Setup3Activity extends BaseSetupActivity {

    private EditText etPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup3);

        etPhone = (EditText) findViewById(R.id.et_phone);

        String phone = mPref.getString("safe_phone", "");
        etPhone.setText(phone);
    }

    @Override
    public void showNextPage() {
        String phone = etPhone.getText().toString().trim();// 注意过滤空格

        if (TextUtils.isEmpty(phone)) {
            // Toast.makeText(this, "安全号码不能为空!", Toast.LENGTH_SHORT).show();
            ToastUtils.showToast(this, "安全号码不能为空!");
            return;
        }

        mPref.edit().putString("safe_phone", phone).commit();// 保存安全号码

        startActivity(new Intent(this, Setup4Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画
    }

    @Override
    public void showPreviousPage() {
        startActivity(new Intent(this, Setup2Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_previous_in,
                R.anim.trans_previous_out);// 进入动画和退出动画
    }

    /**
     * 选择联系人
     * 
     * @param view
     */
    public void selectContact(View view) {
        Intent intent = new Intent(this, ContactActivity.class);//通讯录
        startActivityForResult(intent, 1);//需要有返回值
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // System.out.println("resultCode:" + resultCode);
        // System.out.println("requestCode:" + requestCode);

        if (resultCode == Activity.RESULT_OK) {
            String phone = data.getStringExtra("phone");
            phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替换-和空格

            etPhone.setText(phone);// 把电话号码设置给输入框
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
ToastUtils.java
public class ToastUtils {
    
    public static void showToast(Context ctx,String text){
        Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
    }

}
ContactActivity.java  通讯录 
public class ContactActivity extends Activity {
    private ListView lvList;
    private ArrayList<HashMap<String, String>> readContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        lvList = (ListView) findViewById(R.id.lv_list);

        readContact = readContact();
        // System.out.println(readContact);
        lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item,
                new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone }));
        
        
        //给listview设置监听
        lvList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String phone = readContact.get(position).get("phone");// 读取当前item的电话号码
                Intent intent = new Intent();
                intent.putExtra("phone", phone);
                setResult(Activity.RESULT_OK, intent);// 将数据放在intent中返回给上一个页面

                finish();
            }
        });
    }

    private ArrayList<HashMap<String, String>> readContact() {
        // 首先,从raw_contacts中读取联系人的id("contact_id")
        // 其次, 根据contact_id从data表中查询出相应的电话号码和联系人名称
        // 然后,根据mimetype来区分哪个是联系人,哪个是电话号码
        Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri dataUri = Uri.parse("content://com.android.contacts/data");

        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        // 从raw_contacts中读取联系人的id("contact_id")
//ContentProvider Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[] { "contact_id" }, null, null, null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); // System.out.println(contactId); // 根据contact_id从data表中查询出相应的电话号码和联系人名称, 实际上查询的是视图view_data Cursor dataCursor = getContentResolver().query(dataUri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null); if (dataCursor != null) { HashMap<String, String> map = new HashMap<String, String>(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); // System.out.println(contactId + ";" + data1 + ";" // + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { map.put("phone", data1); } else if ("vnd.android.cursor.item/name".equals(mimetype)) { map.put("name", data1); } } list.add(map); dataCursor.close(); } } rawContactsCursor.close(); } return list; } }

 

activity_setup3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    

    <TextView         
        style="@style/TitleStyle"
        android:text="3.设置安全号码"
        />
    <TextView 
        style="@style/ContentStyle"
        android:text="SIM卡变更后"/>
    <TextView 
        style="@style/ContentStyle"
        android:text="报警短信会发送给安全号码"/>
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入或选择安全号码"
        android:id="@+id/et_phone"
        />
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="请选择联系人"
        android:onClick="selectContact"
        android:background="@drawable/btn_light_green_selector" //状态选择器
        />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            />
           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
        
        
    </LinearLayout>
       
       
      <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <ImageView 
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/setup3"/>
        <Button 
            style="@style/NextStyle"
            />
        <Button 
            style="@style/PreviousStyle"/>
         
            
    </RelativeLayout>
    
    
    
</LinearLayout>

Setup4Activity.java

public class Setup4Activity extends BaseSetupActivity {

    private CheckBox cbProtect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup4);

        cbProtect = (CheckBox) findViewById(R.id.cb_protect);

        boolean protect = mPref.getBoolean("protect", false);

        // 根据sp保存的状态,更新checkbox
        if (protect) {
            cbProtect.setText("防盗保护已经开启");
            cbProtect.setChecked(true);
        } else {
            cbProtect.setText("防盗保护没有开启");
            cbProtect.setChecked(false);
        }

        // 当checkbox发生变化时,回调此方法
        cbProtect.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    cbProtect.setText("防盗保护已经开启");
                    mPref.edit().putBoolean("protect", true).commit();
                } else {
                    cbProtect.setText("防盗保护没有开启");
                    mPref.edit().putBoolean("protect", false).commit();
                }
            }
        });
    }

    @Override
    public void showNextPage() {
        startActivity(new Intent(this, LostFindActivity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画

        mPref.edit().putBoolean("configed", true).commit();// 更新sp,表示已经展示过设置向导了,下次进来就不展示啦
    }

    @Override
    public void showPreviousPage() {
        startActivity(new Intent(this, Setup3Activity.class));
        finish();

        // 两个界面切换的动画
        overridePendingTransition(R.anim.trans_previous_in,
                R.anim.trans_previous_out);// 进入动画和退出动画
    }
}
activity_setup4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
     <TextView         
        style="@style/TitleStyle"
        android:text="4.恭喜您,设置完成"
        />
    <CheckBox 
        android:id="@+id/cb_protect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="防盗保护没有开启"
        />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            />
           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            />
        
        
    </LinearLayout>
       
       
      <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <ImageView 
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/setup3"/>
        <Button 
            style="@style/NextStyle"
            android:text="设置完成"
            />
        <Button 
            style="@style/PreviousStyle"
            
            />
         
            
    </RelativeLayout>

</LinearLayout>


SharePreferences存储在config.xml里面的内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="sim">89014103211118510720</string>
<string name="safe_phone">17852618908</string>
<boolean name="configed" value="true" />
<boolean name="is_show_system" value="true" />
<boolean name="protect" value="true" />
<string name="password">c4</string>
</map>

 



 

posted @ 2017-02-06 18:02  蒙小米  阅读(593)  评论(0编辑  收藏  举报