保存onPause和onResume的状态

时间:2012-06-05 23:04:45

标签: android oncreate onresume instances onpause

我甚至不确定我是否正确地写了标题,因为这是我第一次觉得我必须应对这些方法。

onCreate()中,我使用默认声音打印字符串发出通知。然后,我检查一下我是否得到另一个String。如果它不等于之前的String,我会发出另一个声音通知。也就是说,当我发现我改变了我的位置时,我会发出声音通知。

问题在于我希望我的应用程序在后台运行,但是当我暂停应用程序时,会立即发出声音通知(系统不记得我已经为该字符串发出了通知,它使另一个)。再次,当我打开应用程序时,我再次收到通知。

即使我关闭并打开应用程序100次,我也只想为一个位置发出1次通知。

我该怎么做?

希望我能解释一下这个问题。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.map);
    //here are being called location listeners
    somefunction();
}
public void somefunction(){
//finally, after some operation I get a string
if(newString!=oldString)
    {
        oldString=newString;
        notification(); 
    }
}

public void notification(){
// here I make notification with default audio
}

2 个答案:

答案 0 :(得分:3)

我不知道我是否理解你的问题,但这里的代码可以帮助你。此代码将使应用程序播放一次,然后为应用程序的未来会话保存该值,这意味着除非您保存错误值,否则它将不再播放。

希望它有所帮助!

package stackoverflow.sound;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;

public class PlaySound extends Activity 
{
    private boolean hasSoundBeenPlayed;
    private SharedPreferences settings;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Gets values from last session of your application, check if sound was played!
        // If false no saved value was found (first session of your app)
        this.settings = getPreferences(MODE_PRIVATE);
        this.hasSoundBeenPlayed = settings.getBoolean("hasSoundBeenPlayed", false);

        // your logics.. //

        this.notification();
    }

    public void notification()
    {
        // If sound has been played, do nothing
        if (hasSoundBeenPlayed)
            return;

        this.hasSoundBeenPlayed = true;
        // Play your sound
    }

    // If location was changed, make notifications enabled again
    public void changedLocation()
    {
        this.hasSoundBeenPlayed = false;

        // Do whatever and play again
        this.notification();
    }

    // Called whenever application stops
    protected void onStop()
    {
        super.onStop();

        // Whenever application stops, save the sound boolean for future sessions.
        // If sound has been played (boolean = true), it wont play on any future sessions.
        SharedPreferences.Editor editor = this.settings.edit();
        editor.putBoolean("hasSoundBeenPlayed", this.hasSoundBeenPlayed);
        editor.commit();
     }
}

答案 1 :(得分:1)

使用标记布尔值来记录您是否已经发出声音。在发出声音之前检查此标志是否为假,如果位置再次更改,则将其设置为true。 (不确定我是否正确理解了这个问题)。例如:

boolean alreadySounded = true;

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.map);

    //here are being called location listeners

    checkLocation();    // call both of these in onResume() as well
    checkSounded();

}

private boolean checkLocation() {
    if(newString!=oldString)
    {
        oldString=newString;
        alreadySounded == false;
    }
}

private boolean checkSounded() {
    if(alreadySounded == false)
        notification();
}
相关问题