在线程中开启定时任务,每隔设置的时间段后就会执行相应的动做,timer自己就是一个线程,能够不用放在线程中执行 new Thread(){ @Override public void run() { new Timer().schedule(new TimerTask() { @Override public void run() { } },延迟,时间间隔); } }.start();
判断当前app是否在前台运行,适用于平板的自启动 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (tasks != null && !tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; Log.i("zzm", topActivity.getPackageName()); if (topActivity.getPackageName().equals(pageName)) { return true; } } return false;
而后在本身启动 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); startActivity(intent);