获取app应用日启动次数

package com.testgetapplaunched;
 
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
 
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    private SharedPreferences shPreferences;
    private Editor editor;
    private long bootStartTime;
    private boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bootStartTime=System.currentTimeMillis();
        TextView tv = (TextView)findViewById(R.id.text);
        shPreferences = getApplicationContext().getSharedPreferences("bootcount", Context.MODE_PRIVATE);
        editor = shPreferences.edit();
        /********************************启动事件之启动次数上报**************************************/
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        if(!shPreferences.getString("todaytime","").equals(format.format(System.currentTimeMillis()))){
            flag=true; // 新的一天 开始 了
            editor.putString("todaytime",format.format(System.currentTimeMillis()));
        }
        if(shPreferences.getInt("bootcountnum", 0)>=1){
            int x  = shPreferences.getInt("bootcountnum", 0);
            tv.setText("启动次数:"+x);
            editor.putInt("bootcountnum", x+1);
        }else{
            editor.putInt("bootcountnum", 1);
        }
        if(flag){
            flag=false;
            editor.putInt("bootcountnum", 1);
        }
        editor.putLong("bootStartTime", bootStartTime);
         
        //editor.putString("todaytime", format.format(System.currentTimeMillis())); // 2014 2 21 日
        editor.commit();
        // shijian bijiao  
        // 2014 2 21   2 22  == false
        tv.setText("启动次数:"+shPreferences.getInt("bootcountnum", 0));  
        /********************************启动事件之启动次数上报**************************************/
    }
    public void onClick(View v){
        editor.clear().commit();
    }
 
/*    @Override
    protected void   boolean today = DateUtils.isToday(System.currentTimeMillis());
        System.out.println(today +"今天");  boolean today = DateUtils.isToday(System.currentTimeMillis());
        System.out.println(today +"今天");  boolean today = DateUtils.isToday(System.currentTimeMillis());
        System.out.println(today +"今天");  boolean today = DateUtils.isToday(System.currentTimeMillis());
        System.out.println(today +"今天");aonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        long times = isThisAppUsed(getApplicationContext(),"com.example.onlinetimedemo");
        TextView tv = (TextView)findViewById(R.id.text);
        tv.setText("应用启动次数"+times);
        Log.i("TAG", "应用启动次数"+times);
        //com.testgetapplaunched.MainActivity
        System.out.println(getActivity(getApplicationContext(), "com.testgetapplaunched"));
    }
    
    private String getActivity(Context context,String packageName)
    {
      Intent localIntent = new Intent("android.intent.action.MAIN", null);
      localIntent.addCategory("android.intent.category.LAUNCHER");
      List appList =  context.getPackageManager().queryIntentActivities(localIntent, 0);
      for (int i = 0; i < appList.size(); i++) {
          ResolveInfo resolveInfo = appList.get(i);
          String packageStr = resolveInfo.activityInfo.packageName;
          if (packageStr.equals(packageName)) {
              //这个就是你想要的那个Activity
              Log.i("TAG", resolveInfo.activityInfo.name);
              return resolveInfo.activityInfo.name;
          }
      }
      return "";
    }
 
    public final long isThisAppUsed(Context context,String packageName) {
//        long aUseTime=0;
        int aLaunchCount=0;
        PackageManager pm = context.getPackageManager();
        Intent i = pm.getLaunchIntentForPackage(packageName);
        ComponentName aName = i.getComponent();
        Log.i("TAG", aName.toString());
        try {
 
//            ComponentName aName = new ComponentName(packageName, getActivity(context,packageName));
 
            
 
 
            // 获得ServiceManager类
            Class ServiceManager = Class
                    .forName("android.os.ServiceManager");
 
            // 获得ServiceManager的getService方法
            Method getService = ServiceManager.getMethod("getService",
                    java.lang.String.class);
 
            // 调用getService获取RemoteService
            Object oRemoteService = getService.invoke(null, "usagestats");
 
            // 获得IUsageStats.Stub类
            Class cStub = Class
                    .forName("com.android.internal.app.IUsageStats$Stub");
            // 获得asInterface方法
            Method asInterface = cStub.getMethod("asInterface",
                    android.os.IBinder.class);
            // 调用asInterface方法获取IUsageStats对象
            Object oIUsageStats = asInterface.invoke(null, oRemoteService);
            // 获得getPkgUsageStats(ComponentName)方法
            Method getPkgUsageStats = oIUsageStats.getClass().getMethod(
                    "getPkgUsageStats", ComponentName.class);
            // 调用getPkgUsageStats 获取PkgUsageStats对象
            Object aStats = getPkgUsageStats.invoke(oIUsageStats, aName);
 
            // 获得PkgUsageStats类
            Class PkgUsageStats = Class
                    .forName("com.android.internal.os.PkgUsageStats");
 
            aLaunchCount = PkgUsageStats.getDeclaredField("launchCount")
                    .getInt(aStats);
//            aUseTime = PkgUsageStats.getDeclaredField("usageTime").getLong(
//                    aStats);
 
        } catch (Exception e) {
            Log.e("###", e.toString(), e);
//            e.printStackTrace();
        }
 
//        return aUseTime;
        return aLaunchCount;
    }*/

}



    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

            android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    

你可能感兴趣的:(获取app应用日启动次数)