我有小部件,我正在显示一些数据,我想打开一些活动点击小部件我尝试了谷歌的所有东西,但它不工作。我希望我会从这里得到一些帮助。我是一个小工具中的新手请帮助 提前致谢
public class NewAppWidget extends AppWidgetProvider {
public static RemoteViews views;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context);
CharSequence notetext = NewAppWidgetConfigureActivity.loadNotePref(context);
views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
views.setTextViewText(R.id.wd_note, notetext);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onReceive(Context context, Intent intent) {
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent configIntent = new Intent(context, NewAppWidget.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
答案 0 :(得分:0)
public class NewAppWidget extends AppWidgetProvider {
public static RemoteViews views;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (configIntent.getAction().equals("your_unique_string")) {
/* your logic goes here whatever you want to do onclick of widget view */
startActivity(intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent configIntent = new Intent(context, NewAppWidget.class);
configIntent.setAction("your_unique_string");
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
}
@Override
public void onDisabled(Context context) {
}
}