有没有办法知道最近的应用程序何时开始?

时间:2012-02-26 10:28:46

标签: android android-intent

我正在编写一个应用程序来监控最近启动的应用程序,或者更准确地说 - 测试每个应用程序启动的次数。

为此我正在使用:

final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(20,ActivityManager.RECENT_WITH_EXCLUDED);

for (int i = 0; i < recentTasks.size(); i++) {
Intent baseIntent = recentTasks.get(i).baseIntent;
if(baseIntent != null) {

Log.d("Text", "Application executed: " + i + ": baseIntent: " + baseIntent.getComponent().getPackageName() + baseIntent.getComponent().getClassName());

        }

问题在于它还不够。因为如果一个应用程序启动3次,我将只获得一次。

为此,我需要知道应用程序何时开始。

3 个答案:

答案 0 :(得分:1)

两年后发布:但如果它对您有用:请在此处查看我的答案。

Android how to know an app has been started and range apps priority according the starting times

使用包含所有当前正在运行的应用的列表 taskinfo ,您可以在要跟踪的每个应用的共享首选项中保留计数。因此,您可以监控最近的应用程序以及它们启动的次数。

答案 1 :(得分:0)

一种选择是从logcat中读取日志 并且您可以在活动开始时轻松找到我们。

答案 2 :(得分:0)

package com.example.sample;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RecentTaskInfo;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayList<String> al = new ArrayList<String>();
        Context context = this.getApplicationContext();
        ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
         List<ActivityManager.RecentTaskInfo> recentTasks = mgr.getRecentTasks(100,ActivityManager.RECENT_WITH_EXCLUDED );
         for(Iterator i = recentTasks.iterator(); i.hasNext(); )
            {
            RecentTaskInfo p = (RecentTaskInfo)i.next();     
        String output=p.baseIntent.getComponent().getPackageName();
                al.add(output);
            }

        Button button1=(Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // write on SD card file data in the text box
                try {

                    Log.e("DEBUG", "Recently started tasks");
                    FileWriter outFile = new FileWriter("/sdcard/sdcard/Notes/task.txt");  
                    BufferedWriter outStream = new BufferedWriter(outFile); 
                    for (int k = 0; k < al.size(); k++)  
                    {
                        outStream.write(al.get(k));
                    outStream.write("\n");

                    }
                    outStream.close(); 
                   // System.out.println("Data saved."); 
                 }

        //outStream.write(output);
        //al.add
                        //outStream.write("\n");

                                          //  outStream.close(); 



        catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
                }// onClick
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }}