IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

[Widget] Réalisation d'un compte à rebour


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite Avatar de warwink
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    650
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 650
    Par défaut [Widget] Réalisation d'un compte à rebour
    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 :
    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);
    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

    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
    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"
     />
    AndroidManifest.xml
    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>
    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
    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;
    	}
    }

  2. #2
    Membre émérite Avatar de warwink
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    650
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 650
    Par défaut
    Aucune chance de faire ça si je comprend bien

  3. #3
    Invité de passage
    Inscrit en
    Septembre 2009
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 1
    Par défaut
    Un peu à la bourre mais tanpis je post.
    Si si tu peux tout à fait faire un compte à rebours.
    Regarde ce lien qui te montre comment utiliser Chonometer:

    http://bunjix.fr/utiliser-le-chronom...ns-android/142

    Puis j'ai modifié légèrement la méthode:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public void onChronometerTick(Chronometer chronometer) {
       // TODO Auto-generated method stub
       long elapsedTime = SystemClock.elapsedRealtime() - chronometer.getBase();
       int sec = (int) ((elapsedTime/1000) % 60);
       int seconde=10-sec;
       String time = (seconde==0) ? "10" : String.valueOf(seconde);
       ((Chronometer)findViewById(R.id.chrono)).setText(time);
    }
    Bon courage.

Discussions similaires

  1. [timer] Compte à rebours pour redirection !
    Par Raideman dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 31/12/2005, 20h07
  2. Compte à rebours
    Par Anduriel dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 29/12/2005, 20h12
  3. compte à rebours
    Par Datord dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 17/11/2005, 21h22
  4. compte à rebours
    Par etoile1506 dans le forum C
    Réponses: 10
    Dernier message: 27/10/2005, 15h20
  5. Compte à rebours trop rapide
    Par Anduriel dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 12/06/2005, 20h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo