在Android 4上获取正在运行的名称应用

时间:2016-02-06 15:53:02

标签: android task classname

我试图获取正在运行的应用程序的名称,但似乎它对Android 5无效。

这是我正在使用的代码:

public class Check extends BroadcastReceiver {
Tiempo tt=new Tiempo();
public final String TAG = "CRAR"; // CheckRunningApplicationReceiver

public void onReceive(Context aContext, Intent anIntent) {


try {

// Using ACTIVITY_SERVICE with getSystemService(String)
// to retrieve a ActivityManager for interacting with the global system state.

ActivityManager am = (ActivityManager) aContext
.getSystemService(Context.ACTIVITY_SERVICE);

// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.

List<ActivityManager.RunningTaskInfo> alltasks = am
.getRunningTasks(1);

//
for (ActivityManager.RunningTaskInfo aTask : alltasks) {

// These are showing current running activity in logcat with
// the use of different methods

Log.i(TAG, "===============================");
Log.i(TAG, "aTask.baseActivity: "
+ aTask.baseActivity.flattenToShortString());
Log.i(TAG, "aTask.baseActivity: "
+ aTask.baseActivity.getClassName());
Log.i(TAG, "aTask.topActivity: "
+ aTask.topActivity.flattenToShortString());
Log.i(TAG, "aTask.topActivity: "
+ aTask.topActivity.getClassName());
Log.i(TAG, "===============================");
if(aTask.topActivity.getClassName().compareTo("com.jrdcom.launcher.Launcher")==0){
tt.Detener();
}else{
tt.Contar();
Log.e("tiempo encendido2", String.valueOf(tt.getSegundos()));
}
}

} catch (Throwable t) {
Log.i(TAG, "Throwable caught: "
+ t.getMessage(), t);
}

}
}

当我运行此代码时,logcat会显示:

6240-6240/com.example.usuario.proy I/SR﹕ ===============================
02-06 16:30:11.100    6240-6240/com.example.usuario.proy I/SR﹕ aTask.baseActivity: com.android.launcher/com.android.launcher2.Launcher
02-06 16:30:11.100    6240-6240/com.example.usuario.proy I/SR﹕ aTask.baseActivity: com.android.launcher2.Launcher
02-06 16:30:11.100    6240-6240/com.example.usuario.proy I/SR﹕ aTask.topActivity: com.android.launcher/com.android.launcher2.Launcher
02-06 16:30:11.100    6240-6240/com.example.usuario.proy I/SR﹕ aTask.topActivity: com.android.launcher2.Launcher
02-06 16:30:11.100    6240-6240/com.example.usuario.proy I/SR﹕ ===============================

Asyou可以看到,它只显示启动器,但Android 5中没有应用程序名称。对于Android 4,它可以完美运行。

有什么想法吗?

非常感谢。

0 个答案:

没有答案
相关问题