【问题标题】:Save state for onPause and onResume保存 onPause 和 onResume 的状态
【发布时间】:2012-06-05 23:04:44
【问题描述】:

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

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
}

【问题讨论】:

  • 能否添加一些 onCreate 代码让问题更清晰?

标签: android oncreate onresume instances onpause


【解决方案1】:

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

希望对你有帮助!

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多