Problem with setImageViewUri widget

Asked

Viewed 90 times

3

I tried to create a widget with configuration that displays an image on external storage, everything works as it should, but when restarting the device widgets do not load, show only the app icon.

Here is the Provider code

package aloogle.pokedex.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import aloogle.pokedex.R;

public class WidgetShortcut extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];

            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            final ComponentName cn = new ComponentName("aloogle.pokedex", "aloogle.pokedex.activity.ActivitySplashScreen");
            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_shortcut);

            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.imageView1, pending);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

And of the Config

package aloogle.pokedex.widget;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.io.File;
import aloogle.pokedex.R;

public class WidgetShortcutConfigure extends Activity {

    private WidgetShortcutConfigure context;
    private int widgetID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_shortcut_configure);
        setResult(RESULT_CANCELED);
        context = this;

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
        final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_shortcut);

        final EditText et = (EditText)findViewById(R.id.editText1);
        Button b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String idpk = et.getText().toString();
                File art = new File(Environment.getExternalStorageDirectory() + "/DroiDex/art/sa_" + idpk + ".png");
                String files = art.toString();
                if (art.exists()) {
                    Uri imgUri = Uri.parse(files);
                    views.setImageViewUri(R.id.imageView1, imgUri);
                    Intent intent = new Intent(WidgetShortcutConfigure.this, aloogle.pokedex.activity.ActivitySplashScreen.class);
                    PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

                    views.setOnClickPendingIntent(R.id.imageView1, pending);
                    widgetManager.updateAppWidget(widgetID, views);

                    Intent resultValue = new Intent();
                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
                    setResult(RESULT_OK, resultValue);
                    finish();
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.widgetshortcuttoastwarning), Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });
        setTitle(getString(R.string.addpokemon));
    }
}

That’s how it goes before rebooting and so after.

Does anyone know how to fix this? I know the problem is in onUpdate, only I don’t know the code

  • What if instead of using onUpdate use onStart or maybe a way to use both.

1 answer

1

Implement these methods:

@Override
public void onReceive(Context context, Intent intent) 
{
    super.onReceive(context, intent);
    }

public void onEnabled(Context context) 
{
    super.onEnabled(context);
    }

Log. d in (including onUpdate) and see their call order in logcat.

What runs you put to update the images correctly.

  • 1

    I used Sharedpreferences to save the widget id as a preference with the value that the user put, so this value appears right in onUpdate and worked, but still thanks for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.