Java Android小部件-单击按钮时更新
你好。 我正在尝试创建一个小部件,在屏幕上显示平衡。 创建2个按钮并设置侦听器以确定按下了哪个按钮 难度: 如何通过单击按钮或重新加载小部件来更新数据,以便更新数据?Java Android小部件-单击按钮时更新,java,android,widget,Java,Android,Widget,你好。 我正在尝试创建一个小部件,在屏幕上显示平衡。 创建2个按钮并设置侦听器以确定按下了哪个按钮 难度: 如何通过单击按钮或重新加载小部件来更新数据,以便更新数据? import android.app.AlarmManager; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import andro
import android.app.AlarmManager;
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.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DateFormat;
import java.util.Date;
public class EthWidgetProvider extends AppWidgetProvider {
private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
private static final String SYNC_CLICKED2 = "automaticWidgetSyncButtonClick2";
static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
RequestSingleton.getInstance(context).fetchData(new VolleyCallback() {
@Override
public void onSuccessRequest(JSONArray result) {
Log.i("Response", result.toString());
String price = "";
try {
JSONObject etherObject = result.getJSONObject(0);
price = etherObject.getString("price_usd");
} catch (JSONException e) {
Log.e("JSONException", e.toString());
}
Log.i("Price", price);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_widget);
views.setTextViewText(R.id.gprs, "$" + price);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
views.setTextViewText(R.id.timeofday, currentDateTimeString);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
});
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
watchWidget = new ComponentName(context, EthWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.refresh, getPendingSelfIntent(context, SYNC_CLICKED));
remoteViews.setOnClickPendingIntent(R.id.example_widget_button, getPendingSelfIntent(context, SYNC_CLICKED2));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
for(int appId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (SYNC_CLICKED.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
watchWidget = new ComponentName(context, EthWidgetProvider.class);
remoteViews.setTextViewText(R.id.timeofday, "TESTING");
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
if (SYNC_CLICKED2.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widget);
watchWidget = new ComponentName(context, EthWidgetProvider.class);
remoteViews.setTextViewText(R.id.timeofday, "TESTING2");
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
按下按钮1,我需要显示活动。通过按下第二个按钮,您需要更新小部件,以便更新数据,也就是说,再次向服务器发出请求并更新数据
任何帮助都将不胜感激。我不明白你对广播的想法
public static void updateAppWidget(@NonNull Context applicationContext,
@NonNull Class<? extends AppWidgetProvider> widgetProviderClass) {
final ComponentName component = new ComponentName(applicationContext, widgetProviderClass);
final int[] widgetIds;
try {
widgetIds = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(component);
} catch (RuntimeException re) {
Log.d(re, "Unable to obtain widget IDs.");
return;
}
if (widgetIds.length == 0) {
Log.d("There is no widget to be updated.");
return;
}
final Intent intent = new Intent(applicationContext, widgetProviderClass);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
applicationContext.sendBroadcast(intent);
}
views.setOnClickPendingIntent(R.id.button, pendingIntent);
public static void updateAppWidget(@NonNull Context-applicationContext,
@调用活动的非空类:
remoteViews.setOnClickPendingIntent( R.id.button1, PendingIntent.getActivity(context, 0, new Intent( context, Main.class), 0) );
要从另一个类更新小部件,请执行以下操作:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance( context );
int ids[] = appWidgetManager.getAppWidgetIds( componentName );
for( int id : ids ){
//update textView here
appWidgetManager.updateAppWidget( id, views);
}
有一个非常有用的必读官方指南,标题为“构建应用程序小部件”:
()
请仔细阅读本页,以便您理解概念,并将重点放在解决问题的部分。尽管在本部分中,它们使用:pendingent.getActivity()
,但您应该将服务用作pendingent.getService()
单击按钮调用您的服务,然后让服务更新您的小部件
基本上你会发现你需要:
创建服务
创建悬挂式帐篷:
Intent intent = new Intent(context, YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
在RemoteView对象上设置onClickPendingContent
侦听器,如下所示:
public static void updateAppWidget(@NonNull Context applicationContext,
@NonNull Class<? extends AppWidgetProvider> widgetProviderClass) {
final ComponentName component = new ComponentName(applicationContext, widgetProviderClass);
final int[] widgetIds;
try {
widgetIds = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(component);
} catch (RuntimeException re) {
Log.d(re, "Unable to obtain widget IDs.");
return;
}
if (widgetIds.length == 0) {
Log.d("There is no widget to be updated.");
return;
}
final Intent intent = new Intent(applicationContext, widgetProviderClass);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
applicationContext.sendBroadcast(intent);
}
views.setOnClickPendingIntent(R.id.button, pendingIntent);
谢谢你的回答。我需要更新小部件,而不是从另一个类,而是通过单击小部件本身中的按钮。在这种情况下该怎么办?你的代码在我的情况下不起作用。这很简单:你可以调用服务来更新小部件。然后使用pendingent.getService()我试过这么做,但没能,我能给你发送我的项目代码让你看一次吗?我的目标是创建一个小部件,显示特定资源的余额。小部件在启动时发送请求并显示收到的响应。小部件中会有一个按钮,通过向服务器发送请求来更新数据。我写了他的代码基于我在网络上找到的视频课程和记录。如果你能帮助我,我将非常感激。@AhmadjoniMuhamad我会说,这是一个概念上错误的想法。Widget是非常敏感的东西。更新按钮有点反Widget使用模式。而持续更新也是错误的。对于我来说,我会记录建议您使用SyncAdapter,一些持久性存储和通过widget只需从存储中获取数据并显示它们。然后您可以完全省略该按钮。请参阅:为了避免对服务器造成压力,我决定在正确的时间向用户手动更新数据。您能帮我吗?因此,您将传输bala而不是服务器你为什么要创建widget?在open上与服务器同步的应用程序也会达到同样的目的。它也需要用户点击一下。但它不会对用户的电池造成任何影响。widget通常用于快速操作或预览,而无需任何操作。为了保持屏幕上的平衡,我决定创建一个小部件。我很了解您在创建小部件和在网络上工作方面有经验,如果不困难,请帮助。不要认为这是正确的或不正确的。只要帮助,我将非常高兴您的帮助