简体   繁体   English

Android:如何记录应用程序的使用次数?

[英]Android: How can I keep a record of the number of times an app was used?

I have a very simple app that creates a text file (after a button click) and sends it to a certain email address(after another button click). 我有一个非常简单的应用程序,它创建一个文本文件(按钮点击后)并将其发送到某个电子邮件地址(在另一个按钮点击后)。 I want to add the ability to change the name of the text file that is created based on how many times the file was sent, or ie how many times the app successfully ran till the end. 我想添加更改基于文件发送次数创建的文本文件名称的功能,或者说应用程序成功运行到结束的次数。 Currently, the name of the text file is fixed. 目前,文本文件的名称是固定的。

My idea: 我的想法:

I am thinking of adding a check on start-up of the app to see if another text file exists, lets called it Counter.txt. 我正在考虑添加一个启动应用程序的检查,看看是否存在另一个文本文件,我们称之为Counter.txt。 This will contain the number of times the 'send' button was clicked. 这将包含单击“发送”按钮的次数。 If the file doesn't exist, then it will create it and append the number 0. Every time the 'send' button is clicked, it will open Counter.txt and increment that number. 如果文件不存在,那么它将创建它并附加数字0.每次单击“发送”按钮时,它将打开Counter.txt并递增该数字。 Also on a 'send' click, it will email the main textfile that I want to send and adjust the name by appending the number from Counter.txt to it. 同样在“发送”单击时,它将通过电子邮件发送我要发送的主文本文件,并通过将Counter.txt中的数字附加到其中来调整名称。

I am not sure if this is the best or most efficient method, so would appreciate other suggestions to achieve this. 我不确定这是否是最好或最有效的方法,所以会很感激其他建议来实现这一目标。 Thanks. 谢谢。

Why not use SharedPreferences to store the number of times the app was launched and increment the value on the onCreate() method of your main Activity ? 为什么不使用SharedPreferences存储应用程序启动的次数并增加主ActivityonCreate()方法的值?

Then when the mail is sent, the file is renamed based on the SharedPreferences value. 然后,当发送邮件时,将根据SharedPreferences值重命名该文件。 I think it's better than changing the file name each time the app is started. 我认为这比每次启动应用程序时更改文件名更好。

Here is a good Stack Overflow post on how to use SharedPreferences , you should check it out! 这是关于如何使用SharedPreferences一个很好的Stack Overflow帖子 ,你应该看一下! There is also another post on how to rename a file here . 还有另一篇关于如何在这里重命名文件的帖子。

Hope this helps! 希望这可以帮助!

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. 如果您要保存的密钥值集合相对较少,则应使用SharedPreferences API。 ~ Android Developer Documentation ~ Android开发者文档

// Create your shared preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

// Write to shared preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("yourkey", yourvalue); // You could store the counter right here
editor.commit();

// Read from shared preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 0;
int lastcounter = sharedPref.getInt("yourkey", defaultValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM