我现在正在开发一个简单的小部件。它对我来说真的很新,我真的不知道如何使用AppWidgetProvider
我当前的小部件仅显示图像,当用户点击它时,它将直接链接到网站。
所以,我的问题是,我应该在AppWidgetProvider
使用哪些?
我们知道其中有4个。
onDeleted(context)
onDisabled(context)
onUpdated(context)
onReceived(context)
我目前的代码如下
public class ExampleAppWidgetProvider extends AppWidgetProvider {
private static ImageView img;
public static void updateAppWidget(final Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {
img = (ImageView) findViewById(R.id.Image);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.google.com"));
PendingIntent.getActivity(context, 0, intent, 0);
}
});
}
private static ImageView findViewById(int image) {
// TODO Auto-generated method stub
return null;
}
}
你能看到我犯的任何错误吗?问题是,当我点击它时,图像无法链接到网站。
我在清单中创建了互联网权限。 请帮帮我。
解决方案(我设法使用这些:) tq朋友帮助运行我的项目
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.legoland.com.my"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget1);
views.setOnClickPendingIntent(R.id.Image, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
答案 0 :(得分:2)
在应用小部件中,您需要使用 RemoteViews。[setOnClickPendingIntent ](http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int,android。 app.PendingIntent))
public static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.your_layout);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
v.setOnClickPendingIntent(R.id. Image, PendingIntent.getActivity(context, 0, intent, 0));
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}