با سلام و خسته نباشید . من ازین کد برای نمایش تصویر تصادفی در ویجت استفاده می کنم.اما متاسفانه هیچ اتفاقی نمیوفته.ممنون میشم راهنمایی کنید.
کد پیاچپی:
package ir . akbar . widget ; /** * Created by Akbar on 09/02/2016. */ import android . app . PendingIntent ; import android . appwidget . AppWidgetManager ; import android . appwidget . AppWidgetProvider ; import android . content . Context ; import android . content . Intent ; import java . util . Random ; import android . R . drawable ; import android . app . Activity ; import android . content . res . Resources ; import android . graphics . drawable . Drawable ; import android . os . Bundle ; import android . util . Log ; import android . view . Menu ; import android . view . MenuItem ; import android . view . View ; import android . view . View . OnClickListener ; import android . widget . Button ; import android . widget . ImageView ; import android . widget . RemoteViews ; public class ExampleAppWidgetProvider extends AppWidgetProvider { int max = 16 ; int score = 0 ; String imgName ; Random rand = new Random (); String uri = "@drawable/img1.png" ; public void onUpdate ( Context context , AppWidgetManager appWidgetManager , int [] appWidgetIds ) { super . onUpdate ( context , appWidgetManager , appWidgetIds ); final int N = appWidgetIds . length ; Log . i ( "ExampleWidget" , "Updating widgets " ); for ( int i = 0 ; i < appWidgetIds . length ; i ++) { Random r = new Random (); int min = 1 ; int max = 721 ; int id3 = r . nextInt ( max ) + min ; int id2 = id3 - 1 ; Resources res = context . getResources (); int rndN = rand . nextInt ( 5 ) + 1 ; String imgName = "img" + rndN ;; int id = context . getResources (). getIdentifier ( imgName , "drawable" , context . getPackageName ()); if ( id == id ) { id = context . getResources (). getIdentifier ( imgName , "drawable" , context . getPackageName ()); id2 = id3 - 1 ; } int appWidgetId = appWidgetIds ; Intent intent = new Intent ( context , MainActivity .class); PendingIntent pendingIntent = PendingIntent . getActivity ( context , 0 , intent , 0 ); RemoteViews views = new RemoteViews ( context . getPackageName (), R . layout . widget1 ); views . setImageViewResource ( R . id . image_in_widget , id ); appWidgetManager . updateAppWidget ( appWidgetId , views ); } } }