2 haizeiwangjoin haizeiwangjoin 于 2015.05.26 17:57 提问

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

我的程序锁老是重复解锁界面,下面是主要代码,请大神帮忙如何解决
根据这个类来检测要运行的程序是否要解锁
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
sirnuo   2015.05.27 09:18

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

haizeiwangjoin
haizeiwangjoin 对,但是当输入正确密码后,跳转出来的界面仍然是要让你输入密码的界面
2 年多之前 回复
sirnuo
sirnuo   2015.05.27 20:19

你这样一直运行,不是死循环吗? 无限检查,无限弹界面; 你打印 packageName 看看是什么?

Csdn user default icon
上传中...
上传图片
插入图片