6

我想知道如何记录已启动的活动以用于记录目的。我必须订阅什么广播接收器才能拦截这个意图?或者使用什么意图过滤器?我认为我必须在后台使用某种类型的长时间运行的服务。

我的第一个目标是跟踪主要关注的应用程序,某种历史。

最后想得到一些类似于:

  • - 推出应用程序 com.android.xxx
  • - 推出应用程序 xx.yy.zz
  • - 应用 xx.yy.zz 失去焦点

提前致谢

编辑 - 看看那个应用程序MyAppRank,这正是我的意思

4

2 回答 2

5

我从您的问题中可以看出,您希望在应用程序中启动所有活动时跟踪它们。如果这是正确的,该解决方案可能对您有用:

  1. 创建一个BaseActivity,你的所有活动都应该扩展

    public class BaseActivity extends Activity
    

    {

    private Activity activity;
    
    public static final String INTENTFILTER_TRACK_MY_ACTIVITIES="INTENTFILTER_TRACK_MY_ACTIVITIES";
    public static final String INTENTFILTER_REMOVE_MY_ACTIVITIES="INTENTFILTER_REMOVE_MY_ACTIVITIES";
    
    public void setActivity(Activity act)
    {
        activity = act;
    }
    
    public Activity getActivity()
    {
        return activity;
    }
    
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Intent intent = new Intent();
        intent.setAction(INTENTFILTER_TRACK_MY_ACTIVITIES);
        intent.putExtra("activityName", activity.getClass().getSimpleName());
        sendBroadcast(intent);
    }
    
    @Override protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        Intent intent = new Intent();
        intent.setAction(INTENTFILTER_REMOVE_MY_ACTIVITIES);
        intent.putExtra("activityName", activity.getClass().getSimpleName());
        sendBroadcast(intent);
        setActivity(null);
    }
    

    }

  2. 现在为您的所有活动扩展 BaseActivity 之上。即而不是扩展您的活动应该扩展 BaseActivity 并调用setActivity(this); 在 onCreate 中,如下所示:

    public class MyActivity extends Activity
    

{

     @Override      
      protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setActivity(this);
        //write your other code form here
    }

}

3.然后写一个广播接收器,如下所示:

   class TrackActivitiesReceiver extends BroadcastReceiver
    {

private static final Object SEPERATOR = ",";// use , as seperator 
String sb="";
@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction().equalsIgnoreCase(BaseActivity.INTENTFILTER_TRACK_MY_ACTIVITIES))
    {
        sb+=intent.getStringExtra("activityName");
        sb+=SEPERATOR;
    }
    else if(intent.getAction().equalsIgnoreCase(BaseActivity.INTENTFILTER_REMOVE_MY_ACTIVITIES))
    {
        sb=sb.replace(intent.getStringExtra("activityName")+SEPERATOR, "");
    }
}}  

4最后,在你的AndroidManifest.xml中注册上面的Receiver

    <receiver
        android:name="TrackActivitiesReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="INTENTFILTER_TRACK_MY_ACTIVITIES" />
        </intent-filter>
    </receiver>

希望这能解决您的问题。干杯!

于 2013-07-12T11:09:09.903 回答
1

Intent当应用程序启动或应用程序进入前台时,没有s 广播。没有任何东西可以作为侦听器来获取这些事件。

您可以执行此操作的方式(这是 MyAppRank 等应用程序执行此操作的方式)是使用以下方法ActivityManager

getRunningTasks()
getRunningAppProcesses()
getRecentTasks()

You create a Service which runs all the time and at regular intervals calls methods of the ActvityManager to determine which task is in the foreground and you can "infer" what the user has done (or is doing). It isn't an exact science.

Note: You will need android.permission.GET_TASKS and none of this works anymore as of API 21 (Android 5, Lollipop). As of API 21 the security has been tightened and an application can only get information about its own tasks, not other tasks in the system.

于 2015-06-24T08:06:41.220 回答