android widget setOnClickFillInIntent不启动Activity?

时间:2016-03-17 12:30:12

标签: android listview widget

你能帮助我吗?我真的很累,在点击这个列表中的一些行之后让我的listview转移到主要活动。所以我创建listview然后在我的widget中构建自定义listview所有工作正常但是当我尝试点击listview转移到MainActivity没有任何反应。

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       // context.startService(new Intent(context, WidgetService.class));
        for (int i = 0; i < appWidgetIds.length; i++) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);

            Intent intent = new Intent(context, MainActivity.class);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

            final PendingIntent contactClickPI = PendingIntent.getBroadcast(context,
                    0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setPendingIntentTemplate(R.id.list, contactClickPI);

           /* PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            remoteViews.setPendingIntentTemplate(R.id.widget,
                    pendingIntent);
            remoteViews.setOnClickFillInIntent(R.id.list, pendingIntent);*/
            //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

          //  remoteViews.setPendingIntentTemplate(R.id.list,pendingIntent);

           // remoteViews.setOnClickFillInIntent(R.id.list, intent);
            remoteViews.setRemoteAdapter(R.id.list, new Intent(context, WidgetService.class));
            //remoteViews.setEmptyView(R.id.list, R.id.empty_view);
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
    }

我的RemoteViewsFactory中的代码:

 @Override
    public RemoteViews getViewAt(int position) {
        final Intent i = new Intent();
        final Bundle extra = new Bundle();
       // extra.putString("number", phoneNumber);

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtras(extra);
        views.setOnClickFillInIntent(R.id.widget, i);
}

这是我的布局列表视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B311111F">
    <!--android:background="#F5F5DC"-->
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="widget is Empty"
        android:textColor="@android:color/white"
        android:textSize="12dp"

        />

    <TextView
        android:id="@+id/title_football_app"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:contentDescription="Score Football Title"
        android:gravity="center_horizontal"
        android:text="Score Football"
        android:textColor="@android:color/white"
        android:textSize="16dp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="18dp" />
</FrameLayout>

我的项目在列表视图中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/headerBarLinearLayouts">

    <RelativeLayout
        android:id="@+id/headerBarLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

        <ImageView
            android:id="@+id/image_view_team"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:padding="3dp"
            />

        <TextView
            android:id="@+id/tv_first_team"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/image_view_team"
            android:paddingLeft="7dp"
            android:textColor="@android:color/white"
            android:textSize="9dp"
            android:textStyle="bold|italic"
            android:layout_centerVertical="true"/>
 </RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您应该在此处调用PendingIntent.getActivity()而不是PendingIntent.getBroadcast final PendingIntent contactClickPI = PendingIntent.getBroadcast(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setPendingIntentTemplate(R.id.list, contactClickPI);

你应该使用ListView ITEM中的一些id(你调用ListView容器)views.setOnClickFillInIntent(R.id.widget, i);例如views.setOnClickFillInIntent(R.id.image_view_team, i);好代码:)