【问题标题】:How to deal with AppWidgetProvider in android widget for my project?如何为我的项目处理 android 小部件中的 AppWidgetProvider?
【发布时间】:2012-12-13 08:03:56
【问题描述】:

我现在正在开发一个简单的小部件。它对我来说真的很新,我真的不知道如何使用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);
        }

【问题讨论】:

    标签: android android-widget widget


    【解决方案1】:

    在应用小部件中,您需要使用 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);
    }
    

    【讨论】:

    • 谢谢@andy,这是替代方案之一.. :D 我找到了解决方案及其在我的项目中的工作:) 我在这里 stackoverflow 找到了解决方案。我会将解决方案放在问题的底部。我会尽快测试你的代码:)谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多