Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a very quick question.

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.

So, I thought I would use the Application class:

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.

The problem with that is that the method has never been called before, and it has lots of null objects then. So, there will be a null pointer exception.

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?

Thanks,

Ruchir

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

When your app starts:

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...