我必须检测应用程序从图标启动的次数,或从多任务视图返回到前端的次数。我该怎么做?
答案 0 :(得分:2)
在你的onCreate()方法中,从SharedPreferences读取你的计数器会增加并保存它。
在你的onResume()方法中,对你的其他计数器做同样的事。
答案 1 :(得分:1)
这门课可以帮到你很多
以下是一些示例代码
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
PackageManager pm = ForegroundAppService.this.getPackageManager();
PackageInfo foregroundAppPackageInfo;
foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
Drawable appicon=getPackageManager().getApplicationIcon(foregroundTaskPackageName);
iv.setImageDrawable(appicon); // Log.d("Foreground task ",foregroundTaskAppName); // Log.d("Size ",tasklist.size()+"");
Log.d("App Icon ------------------------------->",appicon+"");
if(Globals.tasklist.size()>=1)
{ String value="";
i=0;
for(HashMap<String, String> map: Globals.tasklist) {
Log.d("map ",map.get("appname"));
for(String str:map.keySet())
{
Log.d("str ",str);
String key=str;
value=map.get(str);
Log.d("Foreground App Name ",foregroundTaskAppName);
Log.d("Existing List Value ",value);
if(value.equals(foregroundTaskAppName))
{
flag=false;
Log.d("time ",map.get("apptime"));
foregroundAppTime = timeIncrement(Integer.parseInt(map.get("apptime")));
map.put("apptime", Integer.toString(foregroundAppTime));
Log.d("time ",map.get("apptime")); //// Log.d("Previous Time ",tasklist.get(key)+"");
HashMap<String,String> hs=new HashMap<String,String>();
hs=Globals.tasklist.get(i);
Set set=hs.entrySet();
Iterator ii = set.iterator();
if(ii.hasNext()) {
Map.Entry me = (Map.Entry)ii.next();
Log.d("Task -----------> ",me.getKey()+"");
Log.d("Time ----------->",me.getValue()+"");
// try // { // foregroundAppTime= timeIncrement((int)me.getValue()); // } // catch(Exception ex){Log.d("Error ",ex.getMessage());} // hs.put("apptime", Integer.toString(foregroundAppTime)); // Globals.tasklist.add(i, hs);
}
// Globals.appinfo.put("appname", foregroundTaskAppName); // Globals.appinfo.put("apptime", Integer.toString(foregroundAppTime)); // Globals.tasklist.add(Globals.appinfo);
break;
}
else
{
flag=true;
}
}
if(flag==false)
{
break;
}
i++;
}
if(flag)
{
Log.d("Foreground App Name in End ",foregroundTaskAppName);
HashMap<String, String> appinfo = new HashMap<String, String>();
appinfo.put("appname", foregroundTaskAppName);
appinfo.put("apptime", "0");
Globals.tasklist.add(appinfo);
}
}
else
{
HashMap<String, String> appinfo = new HashMap<String, String>();
appinfo.put("appname", foregroundTaskAppName);
appinfo.put("apptime", "0");
Globals.tasklist.add(appinfo);
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("Error ",e.getMessage());
}
Log.d("Running Task " ,"---------------Start-----------------");
for(HashMap<String, String> map: Globals.tasklist) {
for(String str:map.keySet())
{
String key=str;
String value=map.get(str);
Log.d("Key ",key);
Log.d("Value ",value);
}
}
Log.d("Running Task " ,"---------------End-----------------");
}}, 0, 1000);
return START_STICKY;
}
int timeIncrement(int time)
{
time++; // Log.d("New Time ",time+"");
return time;
}