활동 및 서비스가 있습니다. 서비스 정수를 참조하고 싶습니다. 서비스 정수가 수시로 업데이트됩니다. 내 문제는 내 활동에서 그 정수를 먼저 값 (예 : 0) 선언 된 가져옵니다.서비스에서 정적 정수 가져 오기
나의 주요 목표는 프로그램을 시작할 때마다 서비스의 업데이트 된 가치를 아는 것입니다.
주요 활동 :
if(Service.doesCounter>0){
//do something
//in this state Service.doesCounter always is 0(checked by log)
}
서비스 :
public static int doesCounter=0; // declared after class as class memeber
//code where I start my method does();
.....
public void does(){
doesCounter++;
Log.e("cccccc","Service Counter "+doesCounter); // everything ok, value is changing as suppose to.
}
편집
내 공유 환경 설정 클래스 :
public class AppPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; // Name of the file -.xml
private SharedPreferences appSharedPrefs;
private Editor prefsEditor;
public AppPreferences(Context context)
{
this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
this.prefsEditor = appSharedPrefs.edit();
}
public boolean getAnything() {
return appSharedPrefs.getBoolean("Anything", false);
}
public void setAnything(Boolean text) {
prefsEditor.putBoolean("Anything", text);
prefsEditor.commit();
}
다음의 주요 활동에서 :
서비스에서 다음public class MainActivity extends Activity {
protected AppPreferences appPrefs;
appPrefs = new AppPreferences(getApplicationContext());
appPrefs.setAnything(fasle);
:
appPrefs = new AppPreferences(getApplicationContext());
그리고이 모든 이전 변경 사항이 reseted되는 일이 발생하면 어떻게 서비스와 MainActivity가 동일한의 환경 설정을 사용할 수 있도록? 어쩌면 어떻게 든 AppPrefs 클래스를 정적으로 만들 수 있습니까?
1. 예, 내 서비스는 별도의 프로세스가 필요하지 않습니다. 왜냐하면 분리하지 않으면 내 앱이 닫힐 때 (사용자가 애플리케이션을 닫을 때 관리되지 않았기 때문입니다.) 사용자가 내 애플리케이션에서 내 애플리케이션과 서비스 둘 다 살해 당했을 때)) 2. 예전에 공유 환경 설정을 사용하고 있었지만 작업 환경에서도 공유 환경 설정을 사용하고있어 문제가 발생하여 활동 및 서비스에 대해 동일한 공유 환경 설정을 사용하지 못했습니다. prefs 그리고 서비스와 mainActivity 둘 다에서 그것의 객체를 생성함으로써 어떻게 든 이미 삽입 된 값들을 재설정 함) – whiteLT
글쎄, 공유 된 환경 설정을 만들기 위해 알아야 할 필요가있다. (이것은 꽤 간단하다.) 다른 프로세스는 서로 다른 메모리 공간을 가지며 활동은 다른 서비스의 메모리에 액세스 할 수 없습니다). – fedepaol
어쩌면 당신은 나를 도울 수 있습니까? 내 편집 게시물을 확인하십시오. – whiteLT