140.s1-Fragment实现程序锁未加锁模块

Fragment中获取到上下文的方法是通过getActivity()获取,程序里面有的没有加锁有的加锁了,可以通过创建一个数据库,没有枷锁的数据库来存放没有加锁的数据库文件。做动画和删除程序所中的文件要注意线程安全问题,可以让先睡觉SystemClock.sleep(5000);,睡的过程中线程锁住。在子线程中刷新UI可以使用runOnUiThread刷新

入口界面activity_advancedtools.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="高级工具"
        />
    <TextView 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:background="@drawable/button"
    	android:text="电话归属地查询"
    	android:textColor="@color/black"
    	android:drawableLeft="@android:drawable/ic_menu_camera"
    	android:gravity="center_vertical"
    	android:textSize="20sp"
    	android:drawablePadding="5dp"
    	android:padding="10dp"
    	android:clickable="true"
    	android:onClick="numberAddressQuery"
        />
    <TextView 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:background="@drawable/button"
    	android:text="短信备份"
    	android:textColor="@color/black"
    	android:drawableLeft="@android:drawable/ic_menu_camera"
    	android:gravity="center_vertical"
    	android:textSize="20sp"
    	android:drawablePadding="5dp"
    	android:padding="10dp"
    	android:clickable="true"
    	android:onClick="backUpsms"
        />    
      <ProgressBar 
          android:id="@+id/progressBar1"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          />
       <TextView 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:background="@drawable/button"
    	android:text="程序锁"
    	android:textColor="@color/black"
    	android:drawableLeft="@android:drawable/ic_menu_camera"
    	android:gravity="center_vertical"
    	android:textSize="20sp"
    	android:drawablePadding="5dp"
    	android:padding="10dp"
    	android:clickable="true"
    	android:onClick="appLock"
        />  

</LinearLayout>

程序锁的主界面activity_applock.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
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="程序锁"
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_unlock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_left_pressed"
            android:gravity="center"
            android:text="未加锁" />

        <TextView
            android:id="@+id/tv_lock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_right_default"
            android:gravity="center"
            android:text="已加锁" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

没有添加程序锁的ListView的显示界面item_lock_fragment.xm

<?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 
	    android:id="@+id/tv_unlock"
	    android:layout_width="match_parent"
    	android:layout_height="wrap_content"
	    />
    <ListView 
        android:id="@+id/list_view"
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
        ></ListView>
</LinearLayout>

程序锁的每一个条目activity_lock.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="horizontal" >
    

    <ImageView 
        android:id="@+id/iv_icon"
        android:layout_width="50dp"
    	android:layout_height="50dp"
    	android:src="@drawable/ic_launcher"
        />
    <TextView 
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:text="应用的名字"
    	android:layout_weight="1111"
        />
    <ImageView 
        android:id="@+id/iv_unlock"
        android:layout_width="50dp"
    	android:layout_height="50dp"
    	android:src="@drawable/list_button_lock_default"
        />
</LinearLayout>

程序锁的创建数据库的文件AppLockOpenHelper.java

package com.ldw.safe.db.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * 创建一个applock的数据库的表,来存放加锁的数据库
 */
public class AppLockOpenHelper extends SQLiteOpenHelper {

	public AppLockOpenHelper(Context context) {
		super(context, "applock.db", null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//数据库中存取包名,包名是唯一的
		db.execSQL("create table info (_id integer primary key autoincrement,packagename varchar(20)) ");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}

数据库的dao文件AppLockDao.java

package com.ldw.safe.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/*
 * 程序所中加了锁的数据库的逻辑文件
 */
public class AppLockDao {

	private AppLockOpenHelper helper;

	public AppLockDao(Context context){
		helper = new AppLockOpenHelper(context);
	}
	
	/*
	 * 添加到程序所中
	 */
	public void add(String packageName){
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("packagename", packageName);
		db.insert("info", null, values);
		db.close();		
	}
	
	/*
	 * 从程序锁里面删除当前的包
	 */
	public void delete(String packageName) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.delete("info", "packagename=?", new String[] { packageName });
		db.close();
	}
	
	/*
     * 查询当前的包是否在程序锁里面
     */
	public boolean find(String packageName) {
		boolean result = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("info", null, "packagename=?",
				new String[] { packageName }, null, null, null);
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		db.close();
		return result;

	}
	
}

程序锁的入口逻辑文件AdvancedToolsActivity.java

package com.ldw.safe.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.ldw.safe.R;
import com.ldw.safe.utils.SmsUtils;
import com.ldw.safe.utils.SmsUtils.BackUpCallBackSms;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/*
 * 高级工具,包含归属地和短信备份
 */
public class AdvancedToolsActivity extends Activity {

	private ProgressDialog pd;
	//注解的形式获取对象
	@ViewInject(R.id.progressBar1)
	private ProgressBar progressBar1;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advancedtools);
        ViewUtils.inject(this);
	}
	
	/*
	 * 归属地查询
	 */
	public void numberAddressQuery(View v){
		startActivity(new Intent(this, AddressActivity.class));
	}
	
	/*
	 * 短信备份的功能
	 */
	public void backUpsms(View v){
		//*换一种进度条的显示方法,ProgressDialog是对话框的形式,不能改变位置,使用布局的形式显示
		pd = new ProgressDialog(AdvancedToolsActivity.this);
		pd.setTitle("提示");
		pd.setMessage("正在备份信息");
		//设置进度条对话框的样式,水平的进度条
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.show();
		
		new Thread(){
			public void run() {
				//传统方法实现进度条
				//boolean result = SmsUtils.backUp(AdvancedToolsActivity.this, pd);
				//接口的方法实现进度条
				boolean result = SmsUtils.backUp(AdvancedToolsActivity.this, new BackUpCallBackSms(){

					@Override
					public void befor(int count) {
						//方法执行前,初始化2个进度条的最大进度
						pd.setMax(count);
						progressBar1.setMax(count);
						
					}

					@Override
					public void onBackUpSms(int process) {
						pd.setProgress(process);
						progressBar1.setProgress(process);
						
					}
					
				});
				
				if(result){
					//子线程不能刷新UI,这种方法子线程弹出Toast
					Looper.prepare();
					Toast.makeText(AdvancedToolsActivity.this, "备份成功", Toast.LENGTH_SHORT).show();
					Looper.loop();
				}else{
					Looper.prepare();
					Toast.makeText(AdvancedToolsActivity.this, "备份失败", Toast.LENGTH_SHORT).show();
					Looper.loop();
				}
				pd.dismiss();
			};
		}.start();

	}
	
	/*
	 * 程序锁
	 */
	public void appLock(View v){
		//点击以后页面跳转
		Intent intent = new Intent(AdvancedToolsActivity.this, AppLockActivity.class);
		startActivity(intent);
	}
}

程序所主界面的逻辑文件,实现程序所显示和没有加锁的显示AppLockActivity.java

package com.ldw.safe.Activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.fragment.LockFragment;
import com.ldw.safe.fragment.UnLockFragment;

public class AppLockActivity extends FragmentActivity implements OnClickListener {

	
	private FrameLayout fl_layout;
	private TextView tv_lock;
	private TextView tv_unlock;
	private UnLockFragment unLockFragment;
	private LockFragment lockFragment;
	private FragmentManager fragmentManager;

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

	}

	private void initUi() {
		setContentView(R.layout.activity_applock);
		
		fl_layout = (FrameLayout)findViewById(R.id.fl_content);
		tv_lock = (TextView) findViewById(R.id.tv_lock); 
		tv_unlock = (TextView) findViewById(R.id.tv_unlock); 
		
		tv_lock.setOnClickListener(this);
		tv_unlock.setOnClickListener(this);
		
		fragmentManager = getSupportFragmentManager();
		FragmentTransaction mTransaction = fragmentManager.beginTransaction();
		
		unLockFragment = new UnLockFragment();
		
		lockFragment = new LockFragment();
		/**
		 * 替换界面
		 * 1 需要替换的界面的id
		 * 2具体指某一个fragment的对象
		 */
		mTransaction.replace(R.id.fl_content, unLockFragment).commit();
		
	}
	
	@Override
	public void onClick(View v){
		FragmentTransaction ft = fragmentManager.beginTransaction();
		
		switch(v.getId()){
		case R.id.tv_unlock:
			 //没有加锁
			tv_unlock.setBackgroundResource(R.drawable.tab_left_pressed);
			tv_lock.setBackgroundResource(R.drawable.tab_right_default);
			//切换
			ft.replace(R.id.fl_content, unLockFragment);
			System.out.println("切换到unlockFragment");
			break;
		case R.id.tv_lock:
			//加锁
			tv_unlock.setBackgroundResource(R.drawable.tab_left_default);
			tv_lock.setBackgroundResource(R.drawable.tab_right_pressed);
			//切换
			ft.replace(R.id.fl_content, lockFragment);
			System.out.println("切换到lockFragment");
			break;
		}
		
		ft.commit();
	}
}

程序锁的逻辑文件UnLockFragment.java

 

 

package com.ldw.safe.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.bean.AppInfo;
import com.ldw.safe.db.dao.AppLockDao;
import com.ldw.safe.engine.AppInfos;

/*
 * 程序未枷锁的逻辑文件
 */
public class UnLockFragment extends Fragment {

	private View view;
	private ListView list_view;
	private TextView tv_unlock;
	private List<AppInfo> appInfos;
	private AppLockDao dao;
	private List<AppInfo> unLockLists;
	private UnLockAdapter adapter;

	/*
	 * 类似activity里面的setContentView
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.item_unlock_fragment, null);
		
		list_view = (ListView) view.findViewById(R.id.list_view);
		tv_unlock = (TextView) view.findViewById(R.id.tv_unlock);
		
		return view;
	}
	
	
	
	@Override
	public void onStart() {
		
		super.onStart();
		//fragment中通过getActivity()获取到上下文
		appInfos = AppInfos.getAppInfos(getActivity());
		
		dao = new AppLockDao(getActivity());
		//定义一个没有加锁的数据库
		unLockLists = new ArrayList<AppInfo>();
			
		for(AppInfo appInfo : appInfos){
			//判断当前的应用是不是在程序锁中
			if(dao.find(appInfo.getApkPackageName())){
				
			}else {
				// 如果查询不到说明没有在程序锁的数据库里面
				unLockLists.add(appInfo);
			}		
		}
					
		adapter = new UnLockAdapter();
		list_view.setAdapter(adapter);
		
	}
	
	public class UnLockAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			tv_unlock.setText("未加锁(" + unLockLists.size() + ")个");
			return unLockLists.size();
		}

		@Override
		public Object getItem(int position) {
			return unLockLists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			final View view;
			final AppInfo appInfo;
			
			if(convertView == null){
				//fragment中通过getActivity()获取到上下文
				view = View.inflate(getActivity(), R.layout.item_unlock, null);
				holder = new ViewHolder();
				holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
				holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
				holder.iv_unlock = (ImageView) view.findViewById(R.id.iv_unlock);
				
				view.setTag(holder);
			}else{
				view = convertView;
				holder = (ViewHolder) view.getTag();
			}
			
			appInfo = unLockLists.get(position);
			
			holder.iv_icon.setImageDrawable(unLockLists.get(position).getIcon());
			holder.tv_name.setText(unLockLists.get(position).getApkName());
			//吧程序所添加到程序锁数据库中
			holder.iv_unlock.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// 初始化一个位移动画
					TranslateAnimation translateAnimation = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 1.0f,
							Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 0);
					// 设置动画时间
					translateAnimation.setDuration(5000);
					// 开始动画,在view上实现动画
					view.startAnimation(translateAnimation);
					
					new Thread(){
						public void run() {
							//线程锁起来,让动画先做再删除
							SystemClock.sleep(5000);
							//主线程刷新UI
							getActivity().runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									//添加到数据库中,app加锁
									dao.add(appInfo.getApkPackageName());
									//没有枷锁的程序就移除一个
									unLockLists.remove(position);
									// 刷新界面
									adapter.notifyDataSetChanged();
								}
							});
					};	
				}.start();
				
				}
			});

			return view;
		}

	}
	
	static class ViewHolder{
		ImageView iv_icon;
		TextView tv_name;
		ImageView iv_unlock;
		
	}
}

 

 

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值