如何在应用程序首次启动时运行特定代码?
我想在我的应用程序第一次启动时运行一些代码,它将过去一周的短信同步到我的网络应用程序。我不确定如何过滤掉一周内的任何内容,但这是我获取所有短信的代码:
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
//upload the recent 1 week sms messages to the server's database
}
我只想运行此代码一次,所以就在第一次打开它时运行。
I want to run some code on the first startup of my application, it is to sync the past week's SMS messages to my web application. I'm unsure of how to filter out anything over a week, but this is my code to get all sms messages:
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
//upload the recent 1 week sms messages to the server's database
}
I want to run this code only once, so right when it is opened for the first time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
SharedPreferences
。例如:Use
SharedPreferences
. For example:您还可以创建一个扩展
Application
的类。这将仅在您的应用程序启动时运行。You can also create a class that extends
Application
. This will then be run at the launch of your app only.