0

タイトルを正しく書いたかどうかさえわかりません。なぜなら、この方法に対処しなければならないと感じるのはこれが初めてだからです。

onCreate()、文字列を出力するデフォルトのサウンドで通知を行います。次に、別のを取得するかどうかを確認しますString。前と同じでない場合はString、音声で別の通知を行います。つまり、自分の居場所を変更したことに気付いたときは、音声で通知します。

問題は、アプリをバックグラウンドで実行したいのですが、アプリを一時停止すると、すぐに音付きの通知が届きます(システムは、その文字列の通知をすでに行ったことを覚えていないため、もう1つ)。また、アプリを開くと、再び通知が届きます。

アプリを100回閉じたり開いたりしても、1つの場所に対して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

私はあなたの質問を完全に理解しているかどうかわかりませんが、ここにあなたを助けるはずのコードがあります。このコードは、アプリケーションを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();
     }
}
于 2012-06-06T00:01:05.710 に答える
1

すでに音を出しているかどうかを記録するフラグブール値を使用します。音を出す前にこのフラグがfalseであることを確認し、場所が再び変わる場合は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();
}
于 2012-06-05T23:18:05.040 に答える