App Widget works but doesn't update

170 views
Skip to first unread message

Thomas C

unread,
May 25, 2017, 11:01:11 AM5/25/17
to android-ndk
I have been following a tutorial to make Android App Widgets, but I have problems.
My widget display my listview but it doesn't update. I don't know why.

Widget activity:

   
 public class WidgetActivity extends Activity {
       
@Override
       
public void onCreate(Bundle state) {
           
super.onCreate(state);
   
           
String word=getIntent().getStringExtra(WidgetProvider.EXTRA_WORD);
   
           
if (word==null) {
                word
="We did not get a word!";
           
}
   
           
Toast.makeText(this, word, Toast.LENGTH_LONG).show();
   
            finish
();
       
}
   
}



Provider:

   
public class WidgetProvider extends AppWidgetProvider {
     
public static String EXTRA_WORD=
       
"com.commonsware.android.appwidget.lorem.WORD";
   
   
@Override
       
public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
                           
int[] appWidgetIds) {
           
for (int i=0; i<appWidgetIds.length; i++) {
               
Intent svcIntent=new Intent(ctxt, WidgetService.class);
   
                svcIntent
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                svcIntent
.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
   
               
RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
                                                  R
.layout.widget_layout);
   
                widget
.setRemoteAdapter(appWidgetIds[i], R.id.widget_listView,
                                      svcIntent
);
   
/*
   
                Calendar c = Calendar.getInstance();
                int currentHour = c.get(Calendar.HOUR_OF_DAY);
                if(currentHour > 19) c.add(Calendar.DATE,1);
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE d MMM ");
                String mNameDate = dateFormat.format(c.getTime());
                widget.setTextViewText(R.id.widget_title,mNameDate );*/

   
   
   
               
Intent clickIntent=new Intent(ctxt, WidgetActivity.class);
               
PendingIntent clickPI=PendingIntent
                                     
.getActivity(ctxt, 0,
                                                    clickIntent
,
                                                   
PendingIntent.FLAG_UPDATE_CURRENT);
   
                widget
.setPendingIntentTemplate(R.id.widget_listView, clickPI);
   
                appWidgetManager
.updateAppWidget(appWidgetIds[i], widget);
           
}
   
           
super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
       
}
   
}



Service:

    public class WidgetService extends RemoteViewsService {
     
@Override
     
public RemoteViewsFactory onGetViewFactory(Intent intent) {
       
return(new WidgetViewsFactory(this.getApplicationContext(),
                                     intent
));
     
}
   
}



Factory

       
 public class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory {
       
private static final String[] items={"lorem", "ipsum", "dolor",
                                           
"sit", "amet", "consectetuer",
                                           
"adipiscing", "elit", "morbi",
                                           
"vel", "ligula", "vitae",
                                           
"arcu", "aliquet", "mollis",
                                           
"etiam", "vel", "erat",
                                           
"placerat", "ante",
                                           
"porttitor", "sodales",
                                           
"pellentesque", "augue",
                                           
"purus"};
       
private List<WidgetEdt> mWedt = new ArrayList<WidgetEdt>();
       
private Calendar c;
       
//private String mDate = "2017-05-03";
       
private JSONArray EdtTab;
       
private Context ctxt=null;
       
Options opt = null;
       
EdtTraitement edt = null;
       
private JSONObject EdtObj = null;
       
private int appWidgetId;
   
       
public WidgetViewsFactory(Context ctxt, Intent intent) {
           
this.ctxt=ctxt;
   
            c
= Calendar.getInstance();
           
//mDay  = c.get(Calendar.DAY_OF_WEEK); //on obtient le  nom jour
           
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //on créé un format pour le lien
           
//mDate = df.format(c.getTime()); //on applique le format
   
            opt
= new Options(ctxt);
            edt
= new EdtTraitement(ctxt);
           
/*
            RemoteViews updateViews = new RemoteViews(ctxt.getPackageName(), R.layout.widget_layout);
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE d MMM ");
            String mNameDate = dateFormat.format(c.getTime());
   
            updateViews.setTextViewText(R.id.widget_title, mNameDate );*/

   
   
            appWidgetId
=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                         
AppWidgetManager.INVALID_APPWIDGET_ID);
       
}
   
       
@Override
       
public void onCreate() {
       
}
   
       
@Override
       
public void onDestroy() {
       
// no-op
       
}
   
       
@Override
       
public int getCount() {
           
out.println("taille: " + mWedt.size());
           
return(mWedt.size());
       
}
   
       
@Override
       
public RemoteViews getViewAt(int position) {
           
RemoteViews row=new RemoteViews(ctxt.getPackageName(),
                                             R
.layout.widget_row);
   
           
WidgetEdt Wedt = mWedt.get(position);
   
           
out.println("update widget 0");
           
//JSONObject EdtObj = EdtTab.getJSONObject(position);
   
            row
.setTextViewText(R.id.widget_jour_heureDeb,Wedt.getHeureDeb() );
            row
.setTextViewText(R.id.widget_jour_heurefin, Wedt.getHeureFin() );
            row
.setTextViewText(R.id.widget_jour_matiere,Wedt.getMatiere() );
            row
.setTextViewText(R.id.widget_jour_salle,Wedt.getSalle() );
   
           
//row.setTextViewText(R.id.widget_jour_matiere, items[position]);
   
           
Intent i=new Intent();
           
Bundle extras=new Bundle();
   
            extras
.putString(WidgetProvider.EXTRA_WORD, items[position]);
            i
.putExtras(extras);
            row
.setOnClickFillInIntent(R.id.widget_jour_matiere, i);
   
   
           
return(row);
       
}
   
       
@Override
       
public RemoteViews getLoadingView() {
       
return(null);
       
}
   
       
@Override
       
public int getViewTypeCount() {
       
return(1);
       
}
   
       
@Override
       
public long getItemId(int position) {
       
return(position);
       
}
   
       
@Override
     
public boolean hasStableIds() {
       
return(true);
     
}
   
       
@Override
       
public void onDataSetChanged() {
           
//if(edt.isOnline()){
               
out.println("update widget 1");
   
               
try {
   
                   
// EdtTab = new JSONArray(edt.getJSONObjectFromURL("http://URL.com/?group=".concat(opt.getGroupStr())));
                   
EdtTab = new JSONArray(edt.getJSONObjectFromURL("http://93.16.98.152/json.txt"));
                   
//out.println("lien: https://edtinfo.alwaysdata.net/server/jour.php?jour=2017-05-15".concat("&groupe=").concat(opt.getGroupStr()));
   
                   
for (int i=0 ; i < EdtTab.length();i++ ){
                       
EdtObj = EdtTab.getJSONObject(i);
                       
//System.out.println("matiere : " + i + " = " + EdtObj.getString("jour"));
                        mWedt
.add(new WidgetEdt(EdtObj.getString("debut"),
                       
EdtObj.getString("fin"),
                       
EdtObj.getString("salle"),
                       
EdtObj.getString("matiere")));
   
                   
}
               
} catch (JSONException e) {
                e
.printStackTrace();
               
} catch (IOException e) {
                e
.printStackTrace();
               
}
               
if(EdtTab.length() == 0 ){
                    mWedt
.add(new WidgetEdt("8:00",
                           
"20:00",
                           
"",
                           
"Vous n\'avez aucun cours."));
               
}
           
//}
       
}
   
       
}



The Widget provider :

   
<?xml version="1.0" encoding="utf-8"?>
   
       
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
           
android:minWidth="160dp"
           
android:minHeight="160dp"
           
android:updatePeriodMillis="180000"
           
android:previewImage="@drawable/widget"
           
android:initialLayout="@layout/widget_layout"
           
android:autoAdvanceViewId="@+id/widget_listView"
           
android:resizeMode="vertical"
           
>
       
</appwidget-provider>



How can I solve my problem ?
Thanks for your help
Thomas
Reply all
Reply to author
Forward
0 new messages