I am developing an android app and I want to know how many times it has been opened. Is there a way to do this?
我正在开发一个Android应用程序,我想知道它被打开了多少次。有没有办法做到这一点?
8 个解决方案
#1
0
1. For a simple approach, keep a text file
where you increment the value by 1, after reading it. Keep the count increment on OnCreate()
method of Activity
1.对于一个简单的方法,请在读取之后保留一个文本文件,将值增加1。保持Activity的OnCreate()方法的计数增量
2. You can use SharedPreference
.
2.您可以使用SharedPreference。
3. Well DataBase
can also be used...but i think that too over-kill for this....
3.数据库也可以使用......但我认为这太过于过分了....
#2
6
In your Application or Activity's onCreate()
method, increment a counter stored in persistent storage such as SharedPreferences
.
在Application或Activity的onCreate()方法中,增加存储在持久存储中的计数器,例如SharedPreferences。
#3
6
Just, declare:
只是,声明:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
Initialize in onCreate(...) :
在onCreate(...)中初始化:
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
Print or count wherever you want (any where in onCreate or any specific click as you specified)
随时随地打印或计数(onCreate中的任何位置或您指定的任何特定点击)
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
Now print totalcount where you want to count eg.:
现在打印您想要计算的总数量,例如:
System.out.println("Total Application counter Reach to :"+totalCount);
#4
5
The problem with using onCreate
in an Activity
is that this will increment the counter even on orientation changes. Using onCreate
in an Application
also has a downside in that your counter will only be incremented when after the VM has closed - so even if the app exits and reopens this will not necessarily increment.
在Activity中使用onCreate的问题是,即使在方向更改时,这也会增加计数器。在应用程序中使用onCreate也有一个缺点,即只有在VM关闭后才会增加计数器 - 所以即使应用程序退出并重新打开,这也不一定会增加。
The truth is there is no fool-proof method for handling this sort of count, however I have come up with a very good way of doing this, that is about as close to 100% accurate as possible. It requires work in both an Application
class and your main Activity
class, and relies on timestamps to differentiate between orientation changes and actual app launches. To start, add the following Application
class:
事实是,没有万无一失的处理这种计数的方法,但是我已经想出了一个非常好的方法来做到这一点,尽可能接近100%准确。它需要在Application类和主Activity类中工作,并依赖时间戳来区分方向更改和实际应用程序启动。首先,添加以下Application类:
/**
* Application class used for correctly counting the number of times an app has been opened.
* @author Phil Brown
* @see <a href="http://*.com/a/22228198/763080">Stack Overflow</a>
*
*/
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();
}
}
You should add this Application to your AndroidManifest.xml
by specifying the name application attribute:
您应该通过指定名称应用程序属性将此应用程序添加到AndroidManifest.xml:
android:name="path.to.CounterApplication"
Now, in your main Activity
, add the following in onCreate
:
现在,在您的主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();
}
#5
3
You can use shared preferences. Every time the app is opened, retrieve the preferences, increment the count, then store it right away. The only issue is that if a user deletes the app along with all preferences, then the count will be erased too. Here is an example of committing to the preferences. Use getPreferences
to retrieve them at the startup of the app.
您可以使用共享首选项。每次打开应用程序时,检索首选项,递增计数,然后立即存储。唯一的问题是,如果用户删除了应用程序以及所有首选项,那么计数也将被删除。以下是提交首选项的示例。使用getPreferences在应用程序启动时检索它们。
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("pref 1", "some text");
editor.commit();
#6
2
One way:
单程:
Keep a property in preferences
and on launching activity
update preference count by '1', but you may not be able to see this increased value because it stays on phone.
将属性保留在首选项中,并将启动活动更新首选项计数设置为“1”,但您可能无法看到此增加的值,因为它保留在手机上。
Otherway
另一种方式
Call a service to your server (if you have any) to increment the visit count.
将服务调用到您的服务器(如果有的话)以增加访问次数。
#7
0
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.
}
#8
0
As I have said in another answer, I think the following is the best solution:
正如我在另一个答案中所说,我认为以下是最佳解决方案:
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
}
}
If we remember the definition of a static variable, we will discover that is perfect for us:
如果我们记住静态变量的定义,我们会发现它对我们来说是完美的:
They are associated with the class, rather than with any object. Every instance of the class shares a class variable.
它们与类相关联,而不是与任何对象相关联。该类的每个实例都共享一个类变量。
When onPause
method or an orientation change is executed the value of valueOfLaunchCountModified
doesn't change; however, if the app process is destroyed, the value of valueOfLaunchCountModified
changes to false.
当执行onPause方法或方向更改时,valueOfLaunchCountModified的值不会更改;但是,如果销毁应用程序进程,则valueOfLaunchCountModified的值将更改为false。
#1
0
1. For a simple approach, keep a text file
where you increment the value by 1, after reading it. Keep the count increment on OnCreate()
method of Activity
1.对于一个简单的方法,请在读取之后保留一个文本文件,将值增加1。保持Activity的OnCreate()方法的计数增量
2. You can use SharedPreference
.
2.您可以使用SharedPreference。
3. Well DataBase
can also be used...but i think that too over-kill for this....
3.数据库也可以使用......但我认为这太过于过分了....
#2
6
In your Application or Activity's onCreate()
method, increment a counter stored in persistent storage such as SharedPreferences
.
在Application或Activity的onCreate()方法中,增加存储在持久存储中的计数器,例如SharedPreferences。
#3
6
Just, declare:
只是,声明:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
Initialize in onCreate(...) :
在onCreate(...)中初始化:
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
Print or count wherever you want (any where in onCreate or any specific click as you specified)
随时随地打印或计数(onCreate中的任何位置或您指定的任何特定点击)
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
Now print totalcount where you want to count eg.:
现在打印您想要计算的总数量,例如:
System.out.println("Total Application counter Reach to :"+totalCount);
#4
5
The problem with using onCreate
in an Activity
is that this will increment the counter even on orientation changes. Using onCreate
in an Application
also has a downside in that your counter will only be incremented when after the VM has closed - so even if the app exits and reopens this will not necessarily increment.
在Activity中使用onCreate的问题是,即使在方向更改时,这也会增加计数器。在应用程序中使用onCreate也有一个缺点,即只有在VM关闭后才会增加计数器 - 所以即使应用程序退出并重新打开,这也不一定会增加。
The truth is there is no fool-proof method for handling this sort of count, however I have come up with a very good way of doing this, that is about as close to 100% accurate as possible. It requires work in both an Application
class and your main Activity
class, and relies on timestamps to differentiate between orientation changes and actual app launches. To start, add the following Application
class:
事实是,没有万无一失的处理这种计数的方法,但是我已经想出了一个非常好的方法来做到这一点,尽可能接近100%准确。它需要在Application类和主Activity类中工作,并依赖时间戳来区分方向更改和实际应用程序启动。首先,添加以下Application类:
/**
* Application class used for correctly counting the number of times an app has been opened.
* @author Phil Brown
* @see <a href="http://*.com/a/22228198/763080">Stack Overflow</a>
*
*/
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();
}
}
You should add this Application to your AndroidManifest.xml
by specifying the name application attribute:
您应该通过指定名称应用程序属性将此应用程序添加到AndroidManifest.xml:
android:name="path.to.CounterApplication"
Now, in your main Activity
, add the following in onCreate
:
现在,在您的主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();
}
#5
3
You can use shared preferences. Every time the app is opened, retrieve the preferences, increment the count, then store it right away. The only issue is that if a user deletes the app along with all preferences, then the count will be erased too. Here is an example of committing to the preferences. Use getPreferences
to retrieve them at the startup of the app.
您可以使用共享首选项。每次打开应用程序时,检索首选项,递增计数,然后立即存储。唯一的问题是,如果用户删除了应用程序以及所有首选项,那么计数也将被删除。以下是提交首选项的示例。使用getPreferences在应用程序启动时检索它们。
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("pref 1", "some text");
editor.commit();
#6
2
One way:
单程:
Keep a property in preferences
and on launching activity
update preference count by '1', but you may not be able to see this increased value because it stays on phone.
将属性保留在首选项中,并将启动活动更新首选项计数设置为“1”,但您可能无法看到此增加的值,因为它保留在手机上。
Otherway
另一种方式
Call a service to your server (if you have any) to increment the visit count.
将服务调用到您的服务器(如果有的话)以增加访问次数。
#7
0
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.
}
#8
0
As I have said in another answer, I think the following is the best solution:
正如我在另一个答案中所说,我认为以下是最佳解决方案:
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
}
}
If we remember the definition of a static variable, we will discover that is perfect for us:
如果我们记住静态变量的定义,我们会发现它对我们来说是完美的:
They are associated with the class, rather than with any object. Every instance of the class shares a class variable.
它们与类相关联,而不是与任何对象相关联。该类的每个实例都共享一个类变量。
When onPause
method or an orientation change is executed the value of valueOfLaunchCountModified
doesn't change; however, if the app process is destroyed, the value of valueOfLaunchCountModified
changes to false.
当执行onPause方法或方向更改时,valueOfLaunchCountModified的值不会更改;但是,如果销毁应用程序进程,则valueOfLaunchCountModified的值将更改为false。