I have a widget app that works with no problem Android Lollipop or lower OS. When I upgraded my nexus 5 into Marshmallow (android 6.0), I realize that my widget has a problem on loading custom row listview. Widget has a dynamic textView and a dynamic listView. textView works perfectly; but there is no hope for listView row items. When I add the widget, rows stuck in loading phase. It's exactly the same code is working on Lollipop or lower devices but not on Marshmallow. I believe Marshmallow is causing a problem that I couldn't find it yet. I'm hoping that someone show me what I'm doing wrong and help me to solve this problem. I'm appreciated all kind of help. Thanks.
WidgetProvider.class:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
System.out.println("WidgetProvider.onUpdate()");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean isMaviKart = MainUtils.getCardType(context).equals(Constants.CARD_TYPE_MAVI);
try {
//set widget id
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
MainUtils.setWidgetID(prefs, allWidgetIds[allWidgetIds.length - 1]);
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intentService = new Intent(context, WidgetService.class);
intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
widget.setRemoteAdapter(appWidgetIds[i], R.id.list_view, intentService);
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.list_view, clickPI);
if (isMaviKart) {
widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTokenPass(prefs)));
widget.setTextViewText(R.id.currency_tl, " Geçiş");
}
else {
widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTotalMoney(prefs)));
widget.setTextViewText(R.id.currency_tl, " TL");
}
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, context.getResources().getString(R.string.error_occured), Toast.LENGTH_SHORT).show();
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
System.out.println("WidgetProvider.onReceive()");
String key = intent.getStringExtra(Constants.EXTRA);
if (key != null)
Log.e("TEST", key);
}
WidgetViewsFactory.class:
@Override
public RemoteViews getViewAt(int position) {
Intent intent = new Intent();
Bundle extras = new Bundle();
rowButton = context.getResources().getIdentifier("widget_button","id", context.getPackageName());
rowText = context.getResources().getIdentifier("widget_textview","id", context.getPackageName());
RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);
// set BUTTON NAMES
row.setTextViewText(rowButton, getFeeTypes(position));
// simple rows:
configureRowsInListview(row, position, context.getResources().getColor(R.color.DeepSkyBlue), View.VISIBLE);
extras.putString(Constants.EXTRA, getEventKey(position));
intent.putExtras(extras);
row.setOnClickFillInIntent(rowButton, intent);
row.setOnClickFillInIntent(rowText, intent);
return row;
}
WidgetService.class:
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return(new WidgetViewsFactory(this.getApplicationContext()));
}
}
MainActivity.class: (keyWord return null on Marshmallow)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String keyWord = getIntent().getStringExtra(Constants.EXTRA);
if (keyWord == null) {
keyWord = "null";
Log.e("onCreate() ", "No data Arrived yet!");
}
}
private void updateWidgetScreen(String updateData, String currencyType) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
remoteViews.setTextViewText(R.id.kalanPara, updateData);
remoteViews.setTextViewText(R.id.currency_tl, currencyType);
Intent intent = new Intent(this, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);
Intent clickIntent = new Intent(this, MainActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
Log.e("updateWidgetScreen", updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs));
}