검색해보니 topActivity 를 이용해서 구하면 되겠구먼... 별거 아니네
private void test100() { ActivityManager activityManager = (ActivityManager)this.getSystemService (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE); for (int i = 0; i < services.size(); i++) { Log.e("xxx",services.get(i).topActivity.toString()); } }
잘되는 것처럼 보임
02-25 19:48:32.346 E 22780 22780 xxx : ComponentInfo{com.sec.android.app.launcher/com.android.launcher2.Launcher} 02-25 19:48:32.346 E 22780 22780 xxx : ComponentInfo{com.example.zdgtest2/com.example.zdgtest2.MainActivity}
위의 예제는 현재의 app에서 확인하는 테스트였다. 그렇다면 다른 app이 올라와 있는 상태라면 어떨까 그래서 broadcast receiver에 등록하여 제대로 처리하는지 알아보았다.
다른 app은 잘읽어 오는지 확인을 위해서 CONNECTIVITY_CHANGE 에 등록하였음
if (action.equals("android.net.conn.CONNECTIVITY_CHANGE")) { NetworkInfo mNetworkInfo; mNetworkInfo = (NetworkInfo) intent.getParcelableExtra("networkInfo"); Log.d(TAG,"info:"+mNetworkInfo.getType() + " :: "+mNetworkInfo.getDetailedState()); ActivityManager activityManager = (ActivityManager)context.getSystemService (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE); for (int i = 0; i < tasks.size(); i++) { Log.e("xxx",tasks.get(i).topActivity.toString()); } }
결과는 이전과 동일... 이게 제대로 나왔다면 동작중인 app의 activity이름이 나와야 하는데...음 아무래도 서비스로 테스트를 해봐야겠음.
02-25 19:48:38.386 E 22780 22780 xxx : ComponentInfo{com.sec.android.app.launcher/com.android.launcher2.Launcher} 02-25 19:48:38.386 E 22780 22780 xxx : ComponentInfo{com.example.zdgtest2/com.example.zdgtest2.MainActivity}
서비스로 변경해서 테스트
public class ServiceClass extends Service{ private Context mContext = null; Thread mThread; Runnable mRun = new Runnable() { public void run() { try{ for(;;){ if( mContext == null ) break; printLog(); Thread.sleep(5000); } }catch(Exception e){ e.printStackTrace(); } } }; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { Log.d("slog", "onStart()"); mContext = this; super.onStart(intent, startId); mThread = new Thread(mRun); mThread.start(); } public void printLog() { ActivityManager activityManager = (ActivityManager)mContext.getSystemService (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE); for (int i = 0; i < tasks.size(); i++) { Log.e("zdg",tasks.get(i).topActivity.toString()); } } @Override public void onDestroy() { Log.d("slog", "onDestroy()"); mContext = null; try { mThread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } super.onDestroy(); } } <service android:name="ServiceClass" > <intent-filter> <action android:name="com.example.zdgtest2.service"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> private void serviceStart() { startService(new Intent("com.example.zdgtest2.service")); }
결과는 에러남 ㅠㅠ
02-26 16:00:46.007 E 11034 11034 AndroidRuntime: FATAL EXCEPTION: main 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: Process: com.example.zdgtest2, PID: 11034 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.example.zdgtest2.service } 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:2052) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:2090) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:2065) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:533) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at com.example.zdgtest2.MainActivity.serviceStart(MainActivity.java:464) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at com.example.zdgtest2.MainActivity.access$12(MainActivity.java:462) 02-26 16:00:46.007 E 11034 11034 AndroidRuntime: at com.example.zdgtest2.MainActivity$1.onItemClick(MainActivity.java:113)
아래와 같이 변경하여 테스트
private void serviceStart() { Intent startIntent = new Intent(this, ServiceClass.class); this.startService(startIntent); //startService(new Intent("com.example.zdgtest2.service")); } 02-26 16:12:24.357 E 12598 14988 zdg : ComponentInfo{com.sec.android.app.launcher/com.android.launcher2.Launcher} 02-26 16:12:29.372 E 12598 14988 zdg : ComponentInfo{com.sec.android.app.launcher/com.android.launcher2.Launcher} 02-26 16:12:34.382 E 12598 14988 zdg : ComponentInfo{com.sec.android.app.launcher/com.android.launcher2.Launcher}
결과가 엉망이네
이쯤에서 포기하고 인터넷 검색질.... 나같은 사람이 있었군
완전 망함
아래 검색 결과 테스트 해보고 사용하시길 바랍니다.
http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l
http://stackoverflow.com/questions/26714285/is-there-an-alternative-for-getrunningtask-api/27304318#27304318
댓글 없음:
댓글 쓰기