haizeiwangjoin 2015-05-26 09:57 采纳率: 0%
浏览 1980

程序锁解决重复解锁的问题

我的程序锁老是重复解锁界面,下面是主要代码,请大神帮忙如何解决
根据这个类来检测要运行的程序是否要解锁
public class lockTask extends TimerTask{

private static Context mContext;
private ActivityManager activityManager;

int i=0;
public lockTask(Context context){
    mContext=context;
    activityManager=(ActivityManager) context.getSystemService("activity");
}
public void run(){
    ComponentName topActivity=activityManager.getRunningTasks(1).get(0).topActivity;
    String packageName=topActivity.getPackageName();//得到要运行的程序包
    boolean result=find(packageName);
    if(result){
        Intent intent=new Intent();
        intent.putExtra("packageName",packageName);
        intent.setClassName("com.example.competition","com.example.competition.unlock");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
}
public boolean find(String packageName){
    boolean result=false;
    SQLiteDatabase db=PwdDAO.getInstance().getHelper().getWritableDatabase();
    if(db.isOpen()){
        Cursor cursor=db.rawQuery("select packageName from tb_pname where packageName=?",new String[]{packageName});
        if(cursor.moveToFirst()){
            result=true;
        }
        cursor.close();
        db.close();
    }
    return result;
}
public static Context getContext(){
    return mContext;
}

}
这是service类
public class lockService extends Service{
private Timer mtimer;
public static final int FOREGROUND_ID=0;

public IBinder onBind(Intent arg0) {
    return null;
}
private void startTimer(){
    if(mtimer==null){
        mtimer=new Timer();
        lockTask locktask=new lockTask(this);
        mtimer.schedule(locktask, 0L,100L);
    }
}
public void onCreate(){
    super.onCreate();
}
public int  onStartCommand(Intent intent,int flags,int startId){
    startForeground(FOREGROUND_ID,new Notification());
    startTimer();
    return super.onStartCommand(intent, flags, startId);
}
public void onDestroy(){   //当停止服务时执行
    stopForeground(true);
    mtimer.cancel();
    mtimer.purge();
    mtimer=null;
    super.onDestroy();
}

}
这是解锁的代码
public class unlock extends ActionBarActivity{
String activityName;
SharedPreferences preference;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Intent intent=getIntent();//得到传递来的意图对象
String pName=intent.getStringExtra("packageName");
setContentView(R.layout.unlock);
preference=getSharedPreferences("password",Context.MODE_PRIVATE);
final EditText et_unlock=(EditText)findViewById(R.id.et_unlock);//获得输入密码的编辑框
Button but_unlock_ok=(Button)findViewById(R.id.but_unlock_ok);
but_unlock_ok.setOnClickListener(new OnClickListener(){

        public void onClick(View v){
            if(TextUtils.isEmpty(et_unlock.getText().toString())){
              Toast.makeText(getBaseContext(),"密码不能为空",Toast.LENGTH_LONG).show();
            }
            else if(!(et_unlock.getText().toString().equals(preference.getString("pwd","")))){
                Toast.makeText(getBaseContext(),"密码输入错误",Toast.LENGTH_LONG).show();
            }
            else{
                String packageName=intent.getStringExtra("packageName");
                Toast.makeText(getBaseContext(),"包名"+packageName,Toast.LENGTH_LONG).show();
                PackageManager pm=getPackageManager();
                Intent resolveIntent=new Intent(Intent.ACTION_MAIN);
                resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                List<ResolveInfo> resolve=pm.queryIntentActivities(resolveIntent,PackageManager.MATCH_DEFAULT_ONLY);
                Collections.sort(resolve,new ResolveInfo.DisplayNameComparator(pm));//将获取到的ResolveInfo进行排序
                for(ResolveInfo reInfo:resolve){
                    if(reInfo.activityInfo.packageName.equals(packageName.toString())){
                        activityName=reInfo.activityInfo.name;//获取应用程序启动activity的名字
                    }
                }
                Intent lanchIntent=new Intent();
                lanchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                lanchIntent.setComponent(new ComponentName(packageName,activityName));
                startActivity(lanchIntent);

            }           
        }
    });
}

}

  • 写回答

2条回答 默认 最新

  • sirnuo 2015-05-27 01:18
    关注

    mtimer.schedule(locktask, 0L,100L); 这行代码不是每个100ms就执行一次 locktask吗??

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建