用于检测应用程序启动的android服务
我正在启动一项服务,该服务检测用户何时启动应用程序,并显示带有应用程序名称的toast用于检测应用程序启动的android服务,android,service,Android,Service,我正在启动一项服务,该服务检测用户何时启动应用程序,并显示带有应用程序名称的toast 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.IB
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");
}
}
package com.xylon.serviceexample;
导入java.util.List;
导入android.app.ActivityManager;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.os.IBinder;
导入android.util.Log;
导入android.widget.Toast;
公共类MyService扩展服务{
私有静态最终字符串TAG=“MyService”;
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
public void onCreate(){
Toast.makeText(这是“恭喜!MyService已创建”,Toast.LENGTH_LONG.show();
Log.d(标记为“onCreate”);
}
@凌驾
公共无效启动(Intent Intent,int startId){
Toast.makeText(这是“我的服务已启动”,Toast.LENGTH_LONG.show();
ActivityManager am=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
List runningAppProcessInfo=am.getRunningAppProcesss();
对于(int i=0;i
我是否已将ActivityManager代码放置在正确的位置?它向我显示了服务启动时设备上已经运行的应用程序。您可以使用读取日志,然后从logcat跟踪 将此添加到清单文件中:
android.permission.READ_LOGS
代码:
但是从API 16开始,谷歌限制了这件事,所以现在我们无法追踪这件事。那么问题出在哪里?
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();
}