我正在为我的安全应用程序创建一项服务,该服务将在每秒检查特定进程是否启动并弹出密码对话框。任何人只有在输入密码时才能使用该应用程序。我的问题是我的计时器反复显示对话框。所以我应用“c”变量来运行这个对话框一次,但这只会在我启动应用程序时出现。在那之后!应用程序直接打开而不先显示对话框`
private static Timer timer = new Timer();
private Context ctx;
private int c=0;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
super.onCreate();
ctx = this;
startService();
}
private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
}
private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}
@Override
public int onStartCommand(Intent intent, int flag,int startId){
/* Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
dial();
}
}
}
}, 0, 1000);*/
return START_STICKY;
}
@Override
public void onDestroy(){
super.onDestroy();
timer.cancel();
Toast.makeText(this, "Service Stoped", Toast.LENGTH_LONG).show();
}
private final Handler toastHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
ActivityManager actvityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.android.music")) {
Toast.makeText(getApplicationContext(), "music is running", Toast.LENGTH_LONG).show();
c++;
if(c==1){
int pid = android.os.Process.getUidForName("com.android.music");
android.os.Process.killProcess(pid);
Intent ii = new Intent();
ii.setClass(APP_SeeL_Password.this,Setappdetail.class);
ii.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);
}
//timer.cancel();
}
}
}
}; `
这是我的对话活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialoge);
LayoutParams params = getWindow().getAttributes();
params.width = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// TODO Auto-generated method stub
EditText et=(EditText)findViewById(R.id.editText2);
str=et.getText().toString();
LockOrNot getpinvar=new LockOrNot(this);
getpinvar.open();
ptr=getpinvar.getpin(1);
getpinvar.close();
Button okb=(Button)findViewById(R.id.button1);
okb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(str.equals(ptr)==true){
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.music");
startActivity( LaunchIntent );}
}
});
Button cancelb=(Button)findViewById(R.id.button2);
cancelb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
});
}