0

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

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
}
4

2 回答 2

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();
     }
}
于 2012-06-06T00:01:05.710 回答
1

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

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();
}
于 2012-06-05T23:18:05.040 回答