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 코드를 넣었습니까? 서비스가 시작될 때 이미 장치에서 실행중인 응용 프로그램을 표시합니다.
그래서 문제가 무엇입니까? – Nizam