2014-04-05 2 views
0

나는 응용 프로그램이 사용자에 의해 시작될 때이를 감지하고 응용 프로그램의 이름으로 축배를 보여주는 서비스를 시작하고 있습니다.applcation의 시작을 감지하는 안드로이드 서비스

package com.xylon.serviceexample; 

import java.util.List; 

import android.app.ActivityManager; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class MyService extends Service{ 

    private static final String TAG = "MyService"; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 

     Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onCreate"); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses(); 
     for (int i = 0; i < runningAppProcessInfo.size(); i++) { 

      Log.v("Proc: ", runningAppProcessInfo.get(i).processName); 
      Toast.makeText(this, runningAppProcessInfo.get(i).processName, Toast.LENGTH_LONG).show(); 
     } 




     Log.d(TAG, "onStart"); 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onDestroy"); 
    } 
} 

올바른 위치에 ActivityManager 코드를 넣었습니까? 서비스가 시작될 때 이미 장치에서 실행중인 응용 프로그램을 표시합니다.

+0

그래서 문제가 무엇입니까? – Nizam

답변

0

당신은 ..

를 READ 로그를 사용하고 로그 캣에서 해당 추적하는 매니페스트 파일이를 추가 할 수 있습니다

android.permission.READ_LOGS 

코드 :

try 
    { 
     Process mLogcatProc = null; 
     BufferedReader reader = null; 
     mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"}); 

     reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream())); 

     String line; 
     final StringBuilder log = new StringBuilder(); 
     String separator = System.getProperty("line.separator"); 

     while ((line = reader.readLine()) != null) 
     { 
      log.append(line); 
      log.append(separator); 
     } 
     String w = log.toString(); 
     Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show(); 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 

하지만 API 16

, Google은이 일을 추적 할 수 없으므로이 일을 제한합니다.

관련 문제