安全卫士第四天笔记

时间:2021-12-05 20:50:42
1、为 引导设置的四个界面添加translate(平移动画)
Android Studio中在res目录下新建一个ainm的文件夹,选择translate,文件名自己随意设置添加动画内容
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
>
<!-- fromXDelta :表示从x轴的哪里开始移动
toXDelta:表示到x轴的哪里结束-->
</translate>
然后在下一步\上一步的点击事件中的finsh();和startActivity()之后,添加这个方法
@Override
public void next_activity() {
Intent intent=new Intent(this,SetUp3Activity.class);
startActivity(intent);
finish();
//R.anim.setup_enter_pre:新界面进入动画
//R.anim.setup_exit_pre:旧界面退出动画
overridePendingTransition(R.anim.setup_enter_next,R.anim.setup_exit_next);
}
@Override
public void per_activity() {
Intent intent=new Intent(this,SetUp1Activity.class);
startActivity(intent);
finish();

overridePendingTransition(R.anim.setup_enter_pre,R.anim.setup_exit_pre);
}
为 引导在设置的四个界面添加触摸屏事件
1在SteUpBaseActivity中获取一个手势识别器
//1.获取手势识别器
gestureDetector = new GestureDetector(this, new NyGestureDetector());
2.实现MygestureDetector类,该继承自GestureDetector.SimpleOnGestureListener
重写OnFling()方法
private class NyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //velocityX :x轴的速率
//触摸的x坐标
int startX = (int) e1.getRawX();
//抬起的x坐标
int endX = (int) e2.getRawX();
int startY = (int) e1.getRawY();
int endY = (int) e2.getRawY();
if ((Math.abs(startY - endY)) > 50) {
return false;
}
if (startX - endX > 50) {
next_activity();
}
if (endX - startX > 50) {
per_activity();
}
return true;
}
}
要想让手势是识别器生效,必须将手势识别器注册到屏幕的触摸事件中
//界面的触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

在第四个界面完成后进入手机防盗界面
public void next_activity() {
SharedPreferences.Editor editor=sp.edit();
editor.putBoolean("first", false);
editor.commit();
Intent intent=new Intent(this,LostFindActivity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.setup_enter_next, R.anim.setup_exit_next);
}
shape资源
在drawable文件下 建一个shape类型的xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="5dp"/>
<gradient android:startColor="#ff0000" android:centerColor="#00ff00" android:endColor="#0000ff"/>
<!--shape:设置形状
corners:设置弧度
gradient:设置渐变色-->
</shape>
引用时用 background +资源地址就行


sim卡的绑定与解除
//找到自定义控件
zidingyi = (SettingView) findViewById(R.id.tv_seeting_zidingyi);
//绑定与解绑SIM卡的操作
zidingyi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sp.edit();
if (zidingyi.booleanBox()) {
//解绑
zidingyi.setBox(false);
sp.getString("sim", "");
} else {
zidingyi.setBox(true);
//捆绑
//获取 SIM信息
TelephonyManager tel = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//获取sim卡的序列号
String sim = tel.getSimSerialNumber();
//保存sim卡的序列号
editor.putString("sim", sim);
}
editor.commit();
}
});
还要加上这么一条权限,因为调用了sim卡信息<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
使用内容解析者获取系统联系人
package ligang.huse.cn.engine;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by javac on 2016/3/1.
*/

public class contactEngine {
public static List<HashMap<String, String>> getAllcontactInfo(Context context) {
HashMap<String, String> hashMap = new HashMap<>();
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
//1.获取内容解析者
ContentResolver resolver = context.getContentResolver();
//2.获取内容提供者的地址:com.android.contacts www.baidu.com/jdk
//raw_contacts表的地址 :raw_contacts
//view_data表的地址 : data
//3.生成查询地址
Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");//http://
Uri date_uri = Uri.parse("content://com.android.contacts/data");
//4.查询操作,先查询raw_contacts,查询contact_id
//projection : 查询的字段
Cursor cursor = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null);
//5.解析cursor
while (cursor.moveToNext()) {
//6.获取查询的数据
String contact_id = cursor.getString(0);
//cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex : 查询字段在cursor中索引值,一般都是用在查询字段比较多的时候
//7.根据contact_id查询view_data表中的数据
//selection : 查询条件
//selectionArgs :查询条件的参数
//sortOrder : 排序
//
if(!TextUtils.isEmpty(contact_id)) {
Cursor c = resolver.query(date_uri, new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{contact_id}, null);
HashMap<String, String> map = new HashMap<String, String>();
//8.解析c
while (c.moveToNext()) {
//9.获取数据
String data1 = c.getString(0);
String mimetype = c.getString(1);
//10.根据类型去判断获取的data1数据并保存
if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {
//电话
map.put("phone", data1);
} else if (mimetype.equals("vnd.android.cursor.item/name")) {
//姓名
map.put("name", data1);
}
}
//11.添加到集合中数据
list.add(map);
//12.关闭cursor
c.close();
}
}
//12.关闭cursor
cursor.close();
return list;
}
}
注:在Android studio中如何测试代码

package ligang.huse.cn.mobilesafe;
import android.test.AndroidTestCase;
import android.util.Log;
import java.util.HashMap;
import java.util.List;
import ligang.huse.cn.engine.contactEngine;
/**
* Created by javac on 2016/3/1.
*/

public class MyTest extends AndroidTestCase {
public void testMy(){
List<HashMap<String, String>> maps = contactEngine.getAllcontactInfo(getContext());
for (HashMap<String,String>list:maps) {
String name = list.get("name");
String number=list.get("phone");
Log.i("---->姓名:",name);
Log.i("---->电话:",number);
}
}
}


设置两个activity之间的通信与数据传递
public void selectContacts(View view){
Intent intent =new Intent(this,contactsActivity.class);
startActivityForResult(intent,0);

}
@Override
//设置回显操作
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String phone=data.getStringExtra("phone");
ed_setup3_number.setText(phone);
}

String phone = list.get(position).get("phone");
Intent intent=new Intent();
//给调用当前activity的activity传递数据
if(intent!=null){
intent.putExtra("phone",phone);
setResult(RESULT_OK,intent);
finish();
}
}



//使用handle与子线程结合解决加载迟缓问题
android.os.Handler handler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
lv_contact.setAdapter(new MyAdapter());
pg.setVisibility(View.INVISIBLE);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
pg = (ProgressBar) findViewById(R.id.pg);
new Thread() {
@Override
public void run() {
list = contactEngine.getAllcontactInfo(getApplicationContext());
handler.sendEmptyMessage(0);
pg.setVisibility(View.VISIBLE);
}
}.start();