开发者

Touchlistener on widget ?

开发者 https://www.devze.com 2023-04-12 04:44 出处:网络
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]appWidgetIds) { for (int appWidgetId : appWidgetIds) {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]  appWidgetIds) {        
    for (int appWidgetId : appWidgetIds) {
       int imageNum = (new java.util.Random().nextInt(IMAGES.length));
       RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.main);
       remoteView.setImageViewResource(R.id.image, IMAGES[imageNum]);
       appWidgetManager开发者_C百科.updateAppWidget(appWidgetId, remoteView);
    }
}

I want use touchListener here on widget so I can change photos. What I should I call or write for that?


Use the function setonClickPendingIntent on remoteView and provide the id of image/button. so that the pending intent will be launched when you click that button.

For example:

PendingIntent pintent = PendingIntent.getBroadcast(/*Input Arguments*/);
remoteView.setonClickPendingIntent(R.id.image,pintent);

Now as we use PendingIntent.getBroadcast to create our pending intent, when it will be launched it will be same like SendBroadcast so we have to implement a onReceive now.

Now Simply put your on Click Logic in onReceive.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号