Android Widget onClick

时间:2011-11-14 21:48:44

标签: android widget onclick

我希望能够点击窗口小部件并启动对话框。我已将官方文档作为一些非官方文档阅读。我最初想要推出一项新活动,但即使这样也失败了。我在Logcat中得到以下内容,但我真的看不到任何东西。

11-14 21:28:47.929: INFO/ActivityManager(116): Starting: Intent { flg=0x10000000 cmp=com.android.app/.Execute bnds=[179,89][300,160] } from pid -1

我猜上面的意思是意图被传递了......但是活动实际上并没有开始。要开始的活动应该是正常活动吗?

使用的代码是:

public class ExampleAppWidgetProvider extends AppWidgetProvider {

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];

        // Create an Intent to launch ExampleActivity
        Intent intent = new Intent(context, ExampleActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
        views.setOnClickPendingIntent(R.id.button, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
}

有什么想法?

1 个答案:

答案 0 :(得分:2)

问题的最可能原因是您尚未在Manifest中声明ExampleActivity

&LT; UPDATE&GT;

您还可以尝试使用唯一编号作为PendingIntent创建的参数2,并在参数4中添加一个合理的标记:

    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT );