Updatefunktion nur EINMAL starten?!

  • Antworten:4
  • Bentwortet
Jack-In-Da-Box
  • Forum-Beiträge: 1.569

01.12.2009, 17:46:22 via Website

hallo leute,

folgendes problem... ich hab mir mal eine kleine testanwendung geschrieben, um eine automatische
updatefunktion eine app zu realisieren... das abfragen des webservers, der versionsvergleich und
die dann folgende auswertung funktionieren (nicht mit diesem quelltext) und sind nicht gegenstand der
frage. mich interessiert vielmehr folgendes...

mit hilfe einer variablen (settings._LastUpdate) lasse ich das update generell nach ablauf einer zeit (später 24std)
starten... oder anders ausgedrückt - die app checkt den angegeben server alle 24 stunden auf updates.
das läuft bereits bei mir!

nun ist es jedoch so, dass wenn man sie app startet, der update-check vollzogen ist UND DANN DAS
DISPLAY GEDREHT wird, startet alles von vorne... mir ist klar, das das nicht das problem ist, wenn
nur alle 24 stunden gecheckt wird... aber wie bekomme ich das (auch auf hinblick für andere projekte) hin,
das etwas (in diesem fall das update) nur EINMAL aufgerufen wird?

mein lösungsansatz war ne STATISCHE VARIABLE (isUpdated)... problem dabei ist, das die bei
erneutem start der app immernoch gesetzt ist - das update (oder was auch immer) nicht ausgeführt wird.
ich muss die app immer erst mit einem taskmanager "killen", bevor das wieder so läuft wie es soll.

wer weiss rat?




1/* #################################################################################### */
2/* public class Main extends Activity
3/* #################################################################################### */
4public class Main extends Activity {
5
6 /* ================================================================================ */
7 /* DECLARES
8 /* ================================================================================ */
9 private Settings settings;
10 /* ================================================================================ */
11 private ImageView ivBackground;
12 /* ================================================================================ */
13 private Handler mHandler;
14 private String html = "";
15 static Boolean isUpdated = false;
16 /* ================================================================================ */
17
18 /* ################################################################################ */
19 /* public void onCreate(Bundle savedInstanceState)
20 /* ################################################################################ */
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.main);
26
27 settings = new Settings();
28 settings.initialize();
29
30 /* ============================================================================ */
31
32 ivBackground = (ImageView) findViewById(R.id.Background);
33 ivBackground.setImageResource(R.drawable.background);
34
35 /* ============================================================================ */
36
37 mHandler = new Handler();
38
39 //if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
40 if (((settings._LastUpdate + (1 * 1000)) < System.currentTimeMillis()) && isUpdated == false) {
41
42 settings._LastUpdate = System.currentTimeMillis();
43 settings.save();
44 isUpdated = true;
45
46 checkUpdate.start();
47
48 }
49
50 }
51
52 /* ################################################################################ */
53 /* public void onResume()
54 /* ################################################################################ */
55 @Override
56 public void onResume() {
57
58 super.onResume();
59
60 }
61
62 /* ################################################################################ */
63 /* public void onPause()
64 /* ################################################################################ */
65 @Override
66 public void onPause() {
67
68 super.onPause();
69
70 }
71
72 /* ################################################################################ */
73 /* public void onDestroy()
74 /* ################################################################################ */
75 @Override
76 public void onDestroy() {
77
78 super.onDestroy();
79
80 }
81
82 /* ################################################################################ */
83 /* public boolean onKeyDown(int keyCode, KeyEvent event)
84 /* ################################################################################ */
85 public boolean onKeyDown(int keyCode, KeyEvent event) {
86
87 switch (keyCode) {
88
89 case KeyEvent.KEYCODE_MENU:
90 finish();
91 break;
92
93 }
94
95 return super.onKeyDown(keyCode, event);
96
97 }
98
99 /* ################################################################################ */
100 /* private Thread checkUpdate
101 /* ################################################################################ */
102 private Thread checkUpdate = new Thread() {
103 public void run() {
104 try {
105
106 URL updateURL = new URL("http://irgendeine.url");
107 URLConnection conn = updateURL.openConnection();
108 InputStream is = conn.getInputStream();
109 BufferedInputStream bis = new BufferedInputStream(is);
110 ByteArrayBuffer baf = new ByteArrayBuffer(50);
111
112 int current = 0;
113 while((current = bis.read()) != -1){
114 baf.append((byte)current);
115 }
116
117 html = new String(baf.toByteArray());
118
119 if (Integer.valueOf(html) > settings._VersionCode) {
120 mHandler.post(showUpdate);
121 }
122
123 } catch (Exception e) {
124
125 }
126 }
127 };
128
129 /* ################################################################################ */
130 /* private Runnable showUpdate
131 /* ################################################################################ */
132 private Runnable showUpdate = new Runnable(){
133 public void run(){
134
135 new AlertDialog.Builder(Main.this)
136 .setIcon(R.drawable.app_icon)
137 .setTitle("Update Available")
138 .setMessage("An update for is available!\n\nOpen Android Market and see the details?")
139
140 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
141 public void onClick(DialogInterface dialog, int whichButton) {
142 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
143 startActivity(intent);
144 }
145 })
146
147 .setNegativeButton("No", new DialogInterface.OnClickListener() {
148 public void onClick(DialogInterface dialog, int whichButton) {
149
150 }
151 })
152
153 .show();
154
155 }
156 };
157 /* ================================================================================ */
158
159 /* ################################################################################ */
160 /* private class Settings
161 /* ################################################################################ */
162 private class Settings {
163
164 /* ============================================================================ */
165 /* DECLARES
166 /* ============================================================================ */
167 private SharedPreferences prefs;
168 /* ============================================================================ */
169 public String _PackageName;
170 public String _VersionName;
171 public Integer _VersionCode;
172 /* ============================================================================ */
173 public Integer _DisplayWidth;
174 public Integer _DisplayHeight;
175 public Integer _DisplayOrientation;
176 /* ============================================================================ */
177 public String _StorageDir;
178 public Boolean _FirstRun;
179 public Long _LastUpdate;
180 /* ============================================================================ */
181
182 /* ############################################################################ */
183 /* public void initialize()
184 /* ############################################################################ */
185 public void initialize() {
186
187 prefs = PreferenceManager.getDefaultSharedPreferences(Main.this);
188
189 try {
190
191 PackageManager manager = Main.this.getPackageManager();
192 PackageInfo info = manager.getPackageInfo(Main.this.getPackageName(), 0);
193 this._PackageName = info.packageName;
194 this._VersionName = info.versionName;
195 this._VersionCode = info.versionCode;
196
197 Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
198 this._DisplayWidth = display.getWidth();
199 this._DisplayHeight = display.getHeight();
200 this._DisplayOrientation = display.getOrientation();
201
202 this._StorageDir = Environment.getExternalStorageDirectory().toString();
203
204 } catch(Exception e) {
205
206 this._PackageName = "com.userscreen.android.alarmdroid";
207 this._VersionName = "1.0";
208 this._VersionCode = 1;
209
210 this._StorageDir = "/sdcard";
211
212 }
213
214 this.load();
215
216 }
217 /* ############################################################################ */
218
219 /* ############################################################################ */
220 /* public void load()
221 /* ############################################################################ */
222 public void load() {
223
224 this._FirstRun = prefs.getBoolean("FirstRun", true);
225 this._LastUpdate = prefs.getLong("LastUpdate", 0);
226
227 }
228 /* ############################################################################ */
229
230 /* ############################################################################ */
231 /* public void save()
232 /* ############################################################################ */
233 public void save() {
234
235 SharedPreferences.Editor pe = prefs.edit();
236 pe.putBoolean("FirstRun", this._FirstRun);
237 pe.putLong("LastUpdate", this._LastUpdate);
238 pe.commit();
239
240 }
241 /* ############################################################################ */
242
243 }
244 /* ================================================================================ */
245
246}
247/* #################################################################################### */

Antworten
Markus Gu
  • Forum-Beiträge: 2.644

01.12.2009, 18:09:57 via Website

überschreibe einfach die methode

onSaveInstanceState(Bundle b)

und speichere dort mittels
b.putBoolean("bereits_gecheckt", isChecked);

und im onCreate liest du dir einfach den wert aus dem bundle

und schon weißt du ob bereits gecheckt wurde oder nicht,

wenn nicht - server checken und sonst einfach nichts tun

swordiApps Blog - Website

Antworten
Jack-In-Da-Box
  • Forum-Beiträge: 1.569

01.12.2009, 18:24:24 via Website

also manchmal *tztztz*
dachte schon an sowas wie "onSaveInstanceState"...

hatte das aber mit folgendem versucht:

1/* ################################################################################ */
2 /* public void onSaveInstanceState(Bundle savedInstanceState)
3 /* ################################################################################ */
4 @Override
5 public void onSaveInstanceState(Bundle savedInstanceState) {
6
7 savedInstanceState.putBoolean("isUpdated", isUpdated);
8 super.onSaveInstanceState(savedInstanceState);
9
10 }
11
12 /* ################################################################################ */
13 /* public void onRestoreInstanceState(Bundle savedInstanceState)
14 /* ################################################################################ */
15 @Override
16 public void onRestoreInstanceState(Bundle savedInstanceState) {
17
18 super.onRestoreInstanceState(savedInstanceState);
19 isUpdated = savedInstanceState.getBoolean("isUpdated");
20
21 }

das brachte aber nichts...
habe nun die "onRestoreInstanceState" weggelassen und folgendes in "onCreate" eingefügt...

1try {
2 isUpdated = savedInstanceState.getBoolean("isUpdated");
3 } catch (Exception e) {
4 isUpdated = false;
5 }


das funktioniert PERFEKT!
VIELEN DANK MARKUS!!!

(manchmal steht man wie im walde... you know?)

— geändert am 01.12.2009, 18:24:41

Antworten
Markus Gu
  • Forum-Beiträge: 2.644

01.12.2009, 20:22:55 via Website

super wenns klappt

gerne

swordiApps Blog - Website

Antworten
Jochen Rühl
  • Forum-Beiträge: 133

02.12.2009, 07:44:05 via Website

Hi,

also eigentlich ist es so, dass Android die Activity automatisch neu startet, wenn sich die Konfiguration ändert, sprich das Display gedreht wird.
Das kannst Du verhindern, indem Du im Manifest über android:configChanges="orientation" spezifizierst, dass Deine Activity die Konfigurationsänderung behandelt.
In Deiner Activity ist dann noch die methode public void onConfigurationChanged(Configuration newConfig) zu überschreiben.

Siehe hierzu auch: Activity restart on rotation Android

Gruß
Jochen

Antworten