【问题标题】:Open Widget Configure from home screen从主屏幕打开小部件配置
【发布时间】:2013-07-04 07:06:30
【问题描述】:

我有一个活动PlaceSearch.java,它也恰好是我的小部件的配置类。我正在开发一个天气小部件,所以我使用FrameLayout,它由ImageButtonLinearLayout 组成。 Please see this link.

当用户点击home screen 上的widget 时,我想打开PlaceSearch.java。所以我使用了下面的代码来实现它

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    System.out.println("entering onUpdate");
    context.startService(new Intent(context, TimeService.class));
    for(int id: appWidgetIds){
        System.out.println(id);
    RemoteViews view = new RemoteViews(context.getPackageName(),R.layout.activity_main);
    Intent intent = new Intent(context,Preferences.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi =PendingIntent.getBroadcast(context, 0, intent, 0);
    view.setOnClickPendingIntent(R.id.ibmainBackground, pi);
    appWidgetManager.updateAppWidget(id, view);
    }
}

但这不起作用可能是因为FrameLayout 导致LinearLayoutImageButton 重叠,因此ImageButton 从未真正被点击过。如果是这样,请提供我打开活动的正确方法。

【问题讨论】:

  • 试试getActivity()而不是getBroadcast()
  • @shoerat 真是个愚蠢的错误……我为此挠了好几个小时……谢谢

标签: android android-intent android-widget android-pendingintent


【解决方案1】:

愚蠢的我不得不写PendingIntent.getActivity(....)。感谢@shoe rat,现在工作正常。对于也尝试从主屏幕打开活动的其他人,请将PendingIntent 添加到基本布局。例如-对我来说是mainLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多