bilal ahmed
bilal ahmed

Reputation: 31

Change imageview in widget dynamically in Android

Can anyone tell me that how can i change images in image view dynamically. i have names of images in database table. After every three seconds a random image should appear in the imageview of widget.

Upvotes: 0

Views: 1351

Answers (1)

King RV
King RV

Reputation: 3770

Try this,

You need to start an alarm which triggers a service after some particular time, and there you update the remote views of your widget...

Put this code in your onUpdate method of your widget class:

AlarmManager alarm;
        final Intent intent = new Intent(context, UpdateService.class);
    final PendingIntent pending = PendingIntent.getService(context, 0,
            intent, 0);
    if (alarm == null) {
        alarm = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        long interval = 500;
        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime(), interval, pending);
    }

and put this code as an inner class in your widget class:

    public static class UpdateService extends Service {

    BatteryInfo mBI = null;

    @Override
    public void onStart(Intent intent, int startId) {
        if (mBI == null) {
            mBI = new BatteryInfo();
            IntentFilter mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(mBI, mIntentFilter);
        }

        RemoteViews updateViews = buildUpdate(this);
        if (updateViews != null) {
            try {
                ComponentName thisWidget = new ComponentName(this,
                        BatteryWidget.class);
                if (thisWidget != null) {
                    AppWidgetManager manager = AppWidgetManager
                            .getInstance(this);
                    if (manager != null && updateViews != null) {
                        manager.updateAppWidget(thisWidget, updateViews);
                    }
                }

            } catch (Exception e) {
                Log.e("Widget", "Update Service Failed to Start", e);
            }
        }

        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            if (mBI != null)
                unregisterReceiver(mBI);
        } catch (Exception e) {
            Log.e("Widget", "Failed to unregister", e);
        }
    }

    /**
     * Build a widget update to show the current Wiktionary
     * "Word of the day." Will block until the online API returns.
     */
    public RemoteViews buildUpdate(Context context) {

        updateViews = new RemoteViews(context.getPackageName(),
                R.layout.widget1);
                updateViews.setImageViewResource(R.id.imgAnim, id);

            }
        return updateViews;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Upvotes: 1

Related Questions