Android中的一个定时任务的方法
生活随笔
收集整理的這篇文章主要介紹了
Android中的一个定时任务的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
在線程中開啟定時任務,每隔設置的時間段后就會執行相應的動作,timer本身就是一個線程,可以不用放在線程中執行 new Thread(){@Overridepublic void run() {new Timer().schedule(new TimerTask() {@Overridepublic 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);轉載于:https://my.oschina.net/u/2987490/blog/902574
總結
以上是生活随笔為你收集整理的Android中的一个定时任务的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 过滤器和监听器
- 下一篇: Java/Android引用类型及其使用