10

I have an app for android which I already made an appwidget for it before lollipop , for some reasons the widget doesn't appear in lollipop. However, it is showing up in the pre-lollipop devices.

Here is my code:

AndroidManifest.xml

<receiver android:name=".widgets.NewsWidgetProvider"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >>
        <intent-filter >
            <action
                android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/news_info" />
</receiver>

news_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_news"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="6000000" >

NewsWidgetProvider.java

public class NewsWidgetProvider extends AppWidgetProvider {
private static final String NEXT_NEWS = "NEXT_NEWS";
private static final String PREVIOUS_NEWS = "PREVIOUS_NEWS";
private static final String GO_TO_NEWS_ACTIVITY = "GO_TO_NEWS_ACTIVITY";
private final String NEWS_LIST_KEY = "newsList";
NewsItem[] news;
int currentNews=0;
Bitmap imageBitmap;
private final String CURRENT_NEWS_KEY = "currentNews";

@Override
public void onUpdate(final Context context, AppWidgetManager appWidgetManager, final int[] appWidgetIds) {

    startService(context);

    loadItemFromSharedPreference(context);

    for (int widgetId : appWidgetIds) {
        setUpView(context, appWidgetManager, appWidgetIds, widgetId);
    }

}


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

    if (news != null) {
        if (GO_TO_NEWS_ACTIVITY.equals(intent.getAction())){
        openNewsActivity(context);
    }

        else if (NEXT_NEWS.equals(intent.getAction())) {
            if (currentNews == news.length - 1) {
                Toast.makeText(context, "reached last news", Toast.LENGTH_SHORT).show();
            }
        else if (news.length - 1 > currentNews) {
            currentNews++;
            SharedPreferencesManager.putInteger(context, CURRENT_NEWS_KEY, currentNews);
            loadItemFromSharedPreference(context);
            setTheCurrentView(context);
        }
        }
    else if (PREVIOUS_NEWS.equals(intent.getAction())) {
                if (currentNews == 0) {
                    Toast.makeText(context, context.getString(R.string.last_item), Toast.LENGTH_SHORT).show();
                }
                else if (currentNews > 0 && news != null) {
                    currentNews--;
                    SharedPreferencesManager.putInteger(context, CURRENT_NEWS_KEY, currentNews);
                    loadItemFromSharedPreference(context);
                    setTheCurrentView(context);
                }
            }

    }
}

private void setUpView(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, int widgetId) {
    final RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_news);

    remoteViews.setTextViewText(R.id.title, news==null?context.getString(R.string.wait_msg):news[currentNews].title);

    remoteViews.setTextViewText(R.id.brief,news==null?"":news[currentNews].brief);
    setPendingIntents(context, appWidgetIds, remoteViews);
    if (news!=null) {
        if (imageBitmap!=null) {
            AQuery aq = new AQuery(context);
            remoteViews.setImageViewBitmap(R.id.image, aq.getCachedImage(news[currentNews].imageUrl, 100));
        }
        else {
            remoteViews.setImageViewResource(R.id.image,R.drawable.ic_launcher);
        }
    }
    else {
        remoteViews.setImageViewResource(R.id.image,R.drawable.ic_launcher);
    }
    appWidgetManager.updateAppWidget(widgetId, remoteViews);
}


private void setTheCurrentView(Context context) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_news);
    remoteViews.setTextViewText(R.id.title, news==null?context.getString(R.string.widget_problem_title):news[currentNews].title);
    remoteViews.setTextViewText(R.id.brief,news==null?context.getString(R.string.widget_problem_breif):news[currentNews].brief);
    remoteViews.setImageViewBitmap(R.id.image, imageBitmap);
    ComponentName thisWidget = new ComponentName( context, NewsWidgetProvider.class );
    AppWidgetManager.getInstance(context).updateAppWidget( thisWidget, remoteViews );
}

private void openNewsActivity(Context context) {
    Intent o=new Intent(context, NewsActivity.class);
    o.putExtra(NewsItem.EXTRA_NEWS_ITEM,news[currentNews]);
    o.putExtra(NewsItem.EXTRA_NEWS_SOURCE_IS_PUSH_OR_WIDGET,true);
    o.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(o);
}

private void setPendingIntents(Context context, int[] appWidgetIds, RemoteViews remoteViews) {
    remoteViews.setOnClickPendingIntent(R.id.next_news, getPendingSelfIntent(context, NEXT_NEWS, appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.previous_news,getPendingSelfIntent(context,PREVIOUS_NEWS,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.title,getPendingSelfIntent(context,GO_TO_NEWS_ACTIVITY,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.brief,getPendingSelfIntent(context,GO_TO_NEWS_ACTIVITY,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.image,getPendingSelfIntent(context,GO_TO_NEWS_ACTIVITY,appWidgetIds));
}

protected PendingIntent getPendingSelfIntent(Context context, String action,int[] appWidgetIds) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            0, intent, 0);
    return pendingIntent;
}
private void loadItemFromSharedPreference(Context context) {

    currentNews=SharedPreferencesManager.getInteger(context, CURRENT_NEWS_KEY, 0);
    news=SharedPreferencesManager.getObject(context, NEWS_LIST_KEY,NewsItem[].class);
    AQuery aq = new AQuery(context);
    imageBitmap= news == null ? BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher) : aq.getCachedImage(news[currentNews].imageUrl,200);
}

private void startService(Context context) {
    Intent broadcastIntent = new Intent(context, NewsWidgetService.class );
    //only start service if news is null
    if (SharedPreferencesManager.getObject(context, NEWS_LIST_KEY, NewsItem[].class)==null)
        context.startService(broadcastIntent);
}

}

The code is working in (kitkat,jelly...) but it's not working in lollipop

Am I missing something?

zaPlayer
  • 787
  • 5
  • 24
  • I have the same bug on Genymotion 4.3 using Google Now Launcher, which is the default launcher in Lollipop, so the problem lies within the launcher and not Lollipop per se – Leo supports Monica Cellio Apr 08 '15 at 14:05
  • I see the same problem with Whatsapp and the Facebook app, but strangely enough not with Evernote – Leo supports Monica Cellio Apr 08 '15 at 14:06
  • What's the device you are running lollipop and trying to show your widget on? – Libelle Aug 21 '16 at 17:39
  • i used nexus 5 and Galaxy s5 @Libelle – zaPlayer Aug 21 '16 at 19:24
  • That shouldn't be a problem. By the way, I noticed that your appwidget-provider tag in news_info.xml doesn't have closing tag. The last line should end with '6000000" />'. (Note the / before closing bracket) – Libelle Aug 22 '16 at 22:40
  • @Samer Zuhair Hi please post your code to load image from url using auery. I'm in urgent need of this. – Sangeetha Dec 21 '16 at 09:57
  • @Sangeetha sorry I no longer have access to that code and have stopped using aquery since that project however, check this link it might help you http://androiddhina.blogspot.com/2015/03/androiddownload-image-using-aquery.html – zaPlayer Dec 26 '16 at 17:31
  • @Samer Zuhair Okay. Thanks for your reply. – Sangeetha Dec 27 '16 at 04:32

3 Answers3

1

I had the same problem - i fixed it by adding additional parameter android:widgetCategory="home_screen" to appwidget-provider:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:configure="com.example.activities.ConfigurationActivity"
    android:previewImage="@drawable/ic_launcher"
    android:initialLayout="@layout/widget_layout"
    android:widgetCategory="home_screen"
    android:resizeMode="none">
</appwidget-provider>
0

This is a bug on Google Now Launcher. I found that a workaround for this is to add a dummy ContentProvider to your app. You can see how to do that on my answer to this other question.

Community
  • 1
  • 1
0

This issue happens with me when installing widgets from Play Store and even when I install directly from Android Studio during development.

Solutions worked for me:

  • Rebooting the device after initial application/appwidget install worked for me.
  • Installing a custom launcher
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
SuperShalabi
  • 471
  • 5
  • 7