做一些应用程序启动时? [英] Do something when app starts?

查看:173
本文介绍了做一些应用程序启动时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常快速的问题。

I have a very quick question.

每当用户下载我的应用程序,由于某种原因,体积为零。我想提出每当用户输入的第一时间的应用程序的数量,和更新的东西。每当创建的应用程序,我希望用户体积将增加,并且用户可以在稍后改变。如果用户离开应用程序(主菜单)和回来,那code应该不会执行。

Whenever the user downloads my app, for some reason the volume is at zero. I want to raise the volume whenever the user enters the app for the first time, and update something. Whenever the app is created, I want the users volume to be increased, and the user can change that later. If the user leaves the app (home menu) and comes back, that code should not execute.

所以,我以为我会使用应用程序类:

这没有工作,因为做我前面提到的更新,我需要调用非静态方法。要做到这一点,我想创建一个类,其中的方法是一个对象,并从那里调用它。

That didn't work, since to do the update I mentioned earlier, I would need to call a non-static method. To do this, I would create an object of the class where the method is, and call it from there.

这样做的问题是,该方法以前从未被调用,它有很多空的对象即可。因此,会有一个空指针异常

那么,如何才能做到这一点?只有拥有量的增加,如果用户已关闭应用程序后回来,或者是第一次用户下载的应用程序。如果应用程序仍然在后台打开无所谓。我还需要在应用程序启动...这将得到一个NullPointerException异常从那里调用另一个方法。

So, how can I achieve this? Only have the volume increase if the user has come back after closing the app or it is the first time the user is downloading the app. Doesn't matter if the app is still open in background. I also need to call another method from there when the app starts...which would get a nullpointerexception.

所以,现在我迷路了。我怎样才能做到这一点?

So now I am lost. How can I achieve this?

谢谢,

Ruchir

推荐答案

实际上改变音量无需任何用户交互将是一个糟糕的用户体验。然而,对于回答这个问题。您可以使用共享preferences

Actually changing volume without any user interaction would be a bad user experience. However for answering this question. You can use SharedPreferences

当您的应用程序启动:

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("com.example.myapp.PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
int defaultValue = 0;
long openedState = sharedPref.getInt("isAppOpenedBefore", defaultValue);

if (defaultValue == openedState)
{
   // First launch
   // Change volume
   // Writing app already opened state
   SharedPreferences.Editor editor = sharedPref.edit();
   editor.putInt("isAppOpenedBefore", 1);
   editor.commit();
}

这篇关于做一些应用程序启动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆