How to manually call `onUpdate` from within the widget itself(如何从小部件本身手动调用`onUpdate`)
问题描述
public class VWid extends AppWidgetProvider {
    public static String WIDGET_UPDATE = "WIDGET_UPDATE";
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (WIDGET_UPDATE.equals(intent.getAction())) {
            Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
        }
    }
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            int k, p;
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int k = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
            Toast.makeText(getActivity(), k+"", Toast.LENGTH_LONG).show();
            Intent imageview2Intent= new Intent(context, VWid.class);
            PendingIntent pendingIntent2= PendingIntent.getBroadcast(context,0, imageview2Intent,0);
            views.setOnClickPendingIntent(R.id.imagebuttonrefresh, pendingIntent2); //
            views.setTextViewText(R.id.textview1, "Last Refresh: " + dateFormat.format(new Date()).toString());
            pushWidgetUpdate(context, views);
        }
    }
    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        ComponentName myWidget = new ComponentName(context, VWid.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews);
        Log.d("UPDATED", "testing");
    }
}
当我第一次将小部件添加到我的主屏幕时,textview1 显示:Last Refresh: {current date and time}.它还在 Toast 中显示系统音量.
When I first add the widget to my homescreen, textview1 displays: Last Refresh: {current date and time}. It also displays the system volume in a Toast.
假设我增加/减少系统音量,当我点击刷新时,onUpdate() 函数应该触发显示更新的 Toast 并将 textview1 文本更新到新的数据和时间,但这并没有发生.
Let's say I increase/decrease the system volume, when I hit refresh the onUpdate() function should fire showing the updated Toast and also updating the textview1 text to the new data and time but that's not happening.
如何修改代码实现刷新,本质上是调用onUpdate()函数.
How can I modify the code so I can achieve a refresh, in essence calling the onUpdate() function.
推荐答案
试试这个:
public class WidgetProvider extends AppWidgetProvider {
 public static final String REFRESH_ACTION = "com.packagename.REFRESH_ACTION";
@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
   if (intent.getAction().equals(WidgetProvider.REFRESH_ACTION)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (appWidgetIds != null && appWidgetIds.length > 0) {
                Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
                this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
            }
        }
    }
}
@Override
public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; ++i) {
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        Intent intentServiceCall = new Intent(context, WidgetService.class);
        intentServiceCall.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
     //Refresh
        Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent.setAction(WidgetProvider.REFRESH_ACTION);
        refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        intentServiceCall.setData(Uri.parse(intentServiceCall.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        rv.setOnClickPendingIntent(R.id.ivRefreshWidget, pendingIntent);
  appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}
                        这篇关于如何从小部件本身手动调用`onUpdate`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从小部件本身手动调用`onUpdate`
				
        
 
            
        基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				