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();
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中获取一个手势识别器
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) {
int startX = (int) e1.getRawX();
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);
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);
TelephonyManager tel = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String sim = tel.getSimSerialNumber();
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;
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>>();
ContentResolver resolver = context.getContentResolver();
Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri date_uri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null);
while (cursor.moveToNext()) {
String contact_id = cursor.getString(0);
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>();
while (c.moveToNext()) {
String data1 = c.getString(0);
String mimetype = c.getString(1);
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);
}
}
list.add(map);
c.close();
}
}
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;
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();
if(intent!=null){
intent.putExtra("phone",phone);
setResult(RESULT_OK,intent);
finish();
}
}
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();