在線程中開啓定時任務,每隔設置的時間段後就會執行相應的動做,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);