2012-06-05 5 views
0

제목을 올바르게 작성했는지 확신 할 수 없습니다.이 메서드에 대처해야한다고 생각하기 때문에 처음으로 올바르게 작성했습니다.onPause 및 onResume의 상태 저장

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 
} 
+0

질문을 명확하게하기 위해 일부 onCreate 코드를 추가 할 수 있습니까? –

답변

3

나는 잘 모릅니다하지만 당신을 도움이 될 것입니다 코드입니다. 이 코드는 응용 프로그램을 한 번 재생 한 다음 응용 프로그램의 이후 세션에서이 값을 저장합니다. 즉, false 값을 저장하지 않으면 다시 재생되지 않습니다.

희망이 있습니다.

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

이미 소리를 내었는지 여부를 기록하는 플래그 부울을 사용하십시오. 사운드를 만들기 전에이 플래그가 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(); 
} 
+0

코드를 입력하십시오. 나는 안드로이드를 처음 접했고 특히이 부분에 익숙하다. – azizbekian

+0

지연에 대해 사과드립니다. 추가 한 코드가 도움이되는지 확인하십시오. – bhekman

관련 문제