Note that there are some explanatory texts on larger screens.

plurals
  1. POWidget update not working properly
    primarykey
    data
    text
    <p>I have a <code>widget</code> which I am updating from my application. The <code>widget</code> is showing a live game score. When there is an update from the server, <code>Widget</code> is updated but it blinks between the previous update and the current update. Like if the score was <code>12-0</code>, and now it is <code>15-0</code>, The widget shows <code>15-0, 12-0, 15-0, 12-0</code> and so on. Please tell me where am I going wrong. Thanks in advance.</p> <p>Here's the code for the widget</p> <pre><code>import java.util.Random; 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.os.Bundle; import android.util.Log; import android.view.View; import android.widget.RemoteViews; import android.widget.TextView; import android.widget.Toast; import android.content.pm.PackageManager; public class MyWidgetProvider extends AppWidgetProvider { public String team1name=""; public String team2name=""; public String team1score="0"; public String team2score="0"; public String player1; public String player2; public String nonStriker; public String role1; public String role2; public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); final int N = appWidgetIds.length; Log.e(MyWidgetProvider.class.toString(), "Onupdate called for " + this); for (int i=0; i&lt;N; i++) { int appWidgetId = appWidgetIds[i]; RemoteViews remoteViews = new RemoteViews(context.getPackageName (), R.layout.widget_layout); ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); appWidgetManager.updateAppWidget(appWidgetId, remoteViews); } } public void onReceive(Context context, Intent intent){ Bundle extras = intent.getExtras(); if (extras == null) { return; } team1name = extras.getString("team1name"); team2name = extras.getString("team2name"); team1score = extras.getString("team1score"); team2score=extras.getString("team2score"); player1=extras.getString("player1"); player2=extras.getString("player2"); nonStriker=extras.getString("nonStriker"); role1=extras.getString("role1"); role2=extras.getString("role2"); team1name=getAcronym(team1name,context); team2name=getAcronym(team2name,context); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); remoteViews.setTextViewText(R.id.team1name, team1name); remoteViews.setTextViewText(R.id.team2name, team2name); remoteViews.setTextViewText(R.id.team1score, team1score); remoteViews.setTextViewText(R.id.team2score, team2score); if(role1!=null &amp;&amp; role1.equalsIgnoreCase("batsman")) { remoteViews.setTextViewText(R.id.batsman1, player1); }else if(role1!=null &amp;&amp; role1.equalsIgnoreCase("bowler")) { remoteViews.setTextViewText(R.id.bowler, player1); } if(role2!=null &amp;&amp; role2.equalsIgnoreCase("batsman")) { remoteViews.setTextViewText(R.id.batsman1, player2); }else if(role2!=null &amp;&amp; role2.equalsIgnoreCase("bowler")) { remoteViews.setTextViewText(R.id.bowler, player2); } remoteViews.setTextViewText(R.id.nonStriker, nonStriker); Intent in = new Intent(Intent.ACTION_MAIN); PackageManager manager = context.getPackageManager(); in = manager.getLaunchIntentForPackage("My Package name"); in.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent pendingIntent = PendingIntent.getActivity(context,0 /* no requestCode */, in, 0 /* no flags */); remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent); ComponentName cn = new ComponentName(context, MyWidgetProvider.class); AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews); } String getAcronym(String teamName,Context context){ if(teamName==null){ return ""; } if(teamName.equals("Australia")){ teamName="AUS"; } else if(teamName.equals("Pakistan")){ teamName="PAK"; } else if(teamName.equals("India")){ teamName="IND"; } else if(teamName.equals("England")){ teamName="ENG"; } else if(teamName.equals("Sri Lanka")){ teamName="SL"; } else if(teamName.equals("South Africa")){ teamName="RSA"; } else if(teamName.equals("West Indies")){ teamName="WI"; } else if(teamName.equalsIgnoreCase("New Zealand")){ teamName="NZ"; } else if(teamName.equals("Zimbabwe")){ teamName="ZIM"; } else if(teamName.equals("Ireland")){ teamName="IRE"; } else if(teamName.equals("Afghanistan")){ teamName="AFG"; } return teamName; } } and Here's the code from the application which is sending the broadcast. if(GameCentre){ watchFromHome.setText("Watch From Home"); intent = new Intent(); intent.setAction("android.intent.action.RUN"); intent.putExtra("team1name", data.get( "vHomeDisplayName") .get( position )); intent.putExtra("team2name", data.get( "vAwayDisplayName") .get( position )); intent.putExtra("team1score", data.get( "vSummary1") .get( position )); intent.putExtra("team2score", data.get( "vSummary2") .get( position )); intent.putExtra("player1", data.get( "vPlayerLastName1" ).get( position )); intent.putExtra("player2", data.get( "vPlayerLastName2" ).get( position )); intent.putExtra("nonStriker", data.get( "vNonStrikerLastName" ).get( position )); intent.putExtra("role1", data.get( "vRole1" ).get( position )); intent.putExtra("role2", data.get( "vRole2" ).get( position )); watchFromHome.setOnClickListener(new OnClickListener(){ public void onClick(View v){ startTimer=true; PlayUpActivity.context.sendBroadcast(intent); } }); if(isLiveMatch){ timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { intent.putExtra("team1name", data.get( "vHomeDisplayName") .get( position )); intent.putExtra("team2name", data.get( "vAwayDisplayName") .get( position )); intent.putExtra("team1score", data.get( "vSummary1") .get( position )); intent.putExtra("team2score", data.get( "vSummary2") .get( position )); intent.putExtra("player1", data.get( "vPlayerLastName1" ).get( position )); intent.putExtra("player2", data.get( "vPlayerLastName2" ).get( position )); intent.putExtra("nonStriker", data.get( "vNonStrikerLastName" ).get( position )); intent.putExtra("role1", data.get( "vRole1" ).get( position )); intent.putExtra("role2", data.get( "vRole2" ).get( position )); PlayUpActivity.context.sendBroadcast(intent); // } } }, 0, 5000); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload