计算我的Android应用程序已打开的次数

我正在开发一个Android应用程序,我想知道它被打开了多少次。 有没有办法做到这一点?

1.对于一个简单的方法,请在读取之后保留一个text file ,将值增加1。 保持Activity的OnCreate()方法的计数增量

2.您可以使用SharedPreference

3. DataBase也可以使用……但我认为这太过于过分了 ….

在Application或Activity的onCreate()方法中,增加存储在持久存储中的计数器,例如SharedPreferences

只是,声明:

  private SharedPreferences prefs; private SharedPreferences.Editor editor; private int totalCount; 

在onCreate(…)中初始化:

  prefs = getPreferences(Context.MODE_PRIVATE); editor = prefs.edit(); 

随时随地打印或计数(onCreate中的任何位置或您指定的任何特定点击)

  totalCount = prefs.getInt("counter", 0); totalCount++; editor.putInt("counter", totalCount); editor.commit(); 

现在打印您想要计算的总数量,例如:

  System.out.println("Total Application counter Reach to :"+totalCount); 

Activity使用onCreate的问题是,即使在方向更改时,这也会增加计数器。 在Application使用onCreate也有一个缺点,即只有在VM关闭后才会增加计数器 – 所以即使应用程序退出并重新打开,这也不一定会增加。

事实是,没有万无一失的处理这种计数的方法,但是我已经想出了一个非常好的方法来做到这一点,尽可能接近100%准确。 它需要在Application类和主Activity类中工作,并依赖时间戳来区分方向更改和实际应用程序启动。 首先,添加以下Application类:

 /** * Application class used for correctly counting the number of times an app has been opened. * @author Phil Brown * @see Stack Overflow * */ public class CounterApplication extends Application { private long lastConfigChange; /** @param buffer the number of milliseconds required after an orientation change to still be considered the same event*/ public boolean wasLastConfigChangeRecent(int buffer) { return (new Date().getTime() - lastConfigChange <= buffer); } @Override public void onCreate() { lastConfigChange = new Date().getTime(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); lastConfigChange = new Date().getTime(); } } 

您应该通过指定名称应用程序属性将此应用程序添加到AndroidManifest.xml

 android:name="path.to.CounterApplication" 

现在,在您的主Activity ,在onCreate添加以下内容:

 //note that you can use getPreferences(MODE_PRIVATE), but this is easier to use from Fragments. SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE); int appOpenedCount = prefs.getInt("app_opened_count", 1); if (!((CounterApplication) getApplication()).wasLastConfigChangeRecent(10000))//within 10 seconds - a huge buffer { appOpenedCount += 1; prefs.edit().putInt("app_opened_count", appOpenedCount).commit(); } //now say you want to do something every 10th time they open the app: boolean shouldDoThing = (appOpenedCount % 10 == 0); if (shouldDoThing) { doThing(); appOpenedCount += 1; //this ensures that the thing does not happen again on an orientation change. prefs.edit().putInt("app_opened_count", appOpenedCount).commit(); } 

您可以使用共享首选项。 每次打开应用程序时,检索首选项,递增计数,然后立即存储。 唯一的问题是,如果用户删除了应用程序以及所有首选项,那么计数也将被删除。 以下是提交首选项的示例。 使用getPreferences在应用程序启动时检索它们。

 SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor=prefs.edit(); editor.putString("pref 1", "some text"); editor.commit(); 

单程:

将属性保留在preferences并将启动activity更新首选项计数设置为“1”,但您可能无法看到此增加的值,因为它保留在手机上。

另一种方式

将服务调用到您的服务器(如果有的话)以增加访问次数。

  Using SharedPreference or the Database. during OnCreate add 1 to the numberofTimes counter and commit. OnCreate (Bundle bundle){ mPref = getPreferences(); int c = mPref.getInt("numRun",0); c++; mPref.edit().putInt("numRun",c).commit(); //do other stuff... } OnCreate is called regardless of you start the app or you resume the app, but isFinishing() returns true if and only iff the user (or you) called finish() on the app (and it was not being destroyed by the manager) This way you only increment when you are doing fresh start. the onFinishing() Method inside of a OnPause method to check to see if the activity is being finish() or just being paused. @Override protected void OnPause(){ if(!onFinishing()){ c = mPref.getInt("numRun",0); c--; mPref.edit().putInt("numRun",c).commit(); } //Other pause stuff. } 

正如我在另一个答案中所说,我认为以下是最佳解决方案:

 private static boolean valueOfLaunchCountModified = false; @Override protected void onCreate(Bundle savedInstanceState) { if(!valueOfCountModified){ preferences = getPreferences(MODE_PRIVATE); launchCount= preferences.getInt("launchCount", 0); if(preferences.edit().putInt("launchCount", ++launchCount).commit()){ valueOfCountModified = true; } } if(launchCount == 5 && valueOfCountModified){ //Do whatever you want } } 

如果我们记住静态变量的定义 ,我们会发现它对我们来说是完美的:

它们与类相关联,而不是与任何对象相关联。 该类的每个实例都共享一个类变量。

当执行onPause方法或方向更改时, valueOfLaunchCountModified的值不会更改; 但是,如果销毁应用程序进程,则valueOfLaunchCountModified的值将更改为false。