我有一个widget
我正在从我的应用程序更新的。正在显示widget
现场比赛比分。当有来自服务器的更新时,Widget
会更新,但它会在上一次更新和当前更新之间闪烁。就像分数是12-0
,现在是15-0
,小部件显示15-0, 12-0, 15-0, 12-0
等等。请告诉我哪里出错了。提前致谢。
这是小部件的代码
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<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 && role1.equalsIgnoreCase("batsman")) {
remoteViews.setTextViewText(R.id.batsman1, player1);
}else if(role1!=null && role1.equalsIgnoreCase("bowler")) {
remoteViews.setTextViewText(R.id.bowler, player1);
}
if(role2!=null && role2.equalsIgnoreCase("batsman")) {
remoteViews.setTextViewText(R.id.batsman1, player2);
}else if(role2!=null && 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);
}
}