Bon, j'ai cherché en français en anglais, rien trouvé, je m'avoue vaincu
Comme écrit dans le titre, j'essaye de faire un widget avec un compte à rebour.
Là plusieurs questions s'imposent :
1) Est ce que le seul moyen de toucher au éléments d'affichage est celui ci :
Ca serait tellement plus simple de pouvoir faire comme dans une Activity, récupérer le TextView, comme ça avec un CountDownTimer, je peux modifier le text, mais même ca est ce que ca serait possible :s
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); views.setTextViewText(R.id.TVWidgetName, data.characterName);
2) J'ai essayé d'utiliser la Class Chronometer, mais pas moyen d'en faire un CountDown, et comme je peux pas faire mon prope Chronometer ...
3) Pour faire mon widget, je me suis basé sur l'exemple de google dans ApiDemos, cependant j'ai des problème au niveau du onUpdate. Il dise que quand on utilise une de classe de Configuration, onUpdate, n'est pas appeler à la création, seulement, avant même que l'activity configuration s'affice, il en fait un.
Puis après le onUpdate automatique n'est jamais rappelé ....
Avez vous des idées ? savez vous comment faire ? Est ce que je me complique la vie ?
Mes fichiers si cela peut aider. J'ai enlevé le code métier sans importance ..
res/xml/widget.xml
AndroidManifest.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="186dp" android:minHeight="45dp" android:updatePeriodMillis="120000" android:initialLayout="@layout/appwidget_provider" android:configure="com.eveinfo.widget.WidgetConfigure" />
WidgetProvider.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <activity android:name=".widget.WidgetConfigure"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity> <receiver android:name=".widget.WidgetProvider"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /> </receiver>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class WidgetProvider extends AppWidgetProvider { // log tag private static final String TAG = "WidgetProvider"; private static CountDown countDown; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { System.out.println("onUpdate"); Log.d(TAG, "onUpdate"); final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { Log.d(TAG, "onDeleted"); final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { WidgetConfigure.deleteTitlePref(context, appWidgetIds[i]); } } @Override public void onEnabled(Context context) { Log.d(TAG, "onEnabled"); } @Override public void onDisabled(Context context) { Log.d(TAG, "onDisabled"); } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId); WidgetData data = WidgetConfigure.loadTitlePref(context, appWidgetId); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); ....... appWidgetManager.updateAppWidget(appWidgetId, views); } static class CountDown extends CountDownTimer { private Context context; private long time; public CountDown(Context context, long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); this.context = context; } @Override public void onFinish() { } @Override public void onTick(long millisUntilFinished) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); views.setTextViewText(R.id.TVWidgetTraining, "" + millisUntilFinished); setTime(millisUntilFinished); } public void setTime(long time) { this.time = time; } public long getTime() { return time; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 package com.eveinfo.widget; /* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.eveinfo.R; import com.eveinfo.tools.ConfigFileException; import com.eveinfo.tools.Pilot; import com.eveinfo.tools.Setting; // Need the following import to get access to the app resources, since this // class is in a sub-package. /** * The configuration screen for the ExampleAppWidgetProvider widget sample. */ public class WidgetConfigure extends Activity { static final String TAG = "AppWidgetConfigure"; private static final String PREFS_NAME = "com.eveinfo.widget.WidgetProvider"; private static final String PREF_PREFIX_KEY = "widget_"; private static final String PREF_SUFIX_CHARNAME_KEY = "_charName"; private static final String PREF_SUFIX_USERID_KEY = "_userID"; private static final String PREF_SUFIX_CHARID_KEY = "_charID"; private static final String PREF_SUFIX_APIID_KEY = "_apiKey"; private Spinner spinner; private ArrayList<Pilot> pilots; private String[] list; int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; public WidgetConfigure() { super(); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setResult(RESULT_CANCELED); setContentView(R.layout.appwidget_configure); ......... spinner = (Spinner) findViewById(R.id.SWidget); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); findViewById(R.id.BWidgetSave).setOnClickListener(mOnClickListener); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } } View.OnClickListener mOnClickListener = new View.OnClickListener() { public void onClick(View v) { final Context context = WidgetConfigure.this; Pilot pilot = pilots.get(spinner.getSelectedItemPosition()); saveTitlePref(context, mAppWidgetId, pilot.getName(), pilot.getUserId(), pilot.getCharacterId(), pilot .getApiKey()); // Push widget update to surface with newly set prefix AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); WidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId); // Make sure we pass back the original appWidgetId Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish(); } }; static void saveTitlePref(Context context, int appWidgetId, String charName, int userID, long charID, String apiKey) { ......... } static WidgetData loadTitlePref(Context context, int appWidgetId) { ......... } static void deleteTitlePref(Context context, int appWidgetId) { } static void loadAllTitlePrefs(Context context, ArrayList<Integer> appWidgetIds, ArrayList<String> texts) { } public static class WidgetData { String characterName; int userID; long characterID; String apiKey; } }
Partager