【问题标题】:listview in app widget android应用小部件android中的列表视图
【发布时间】:2013-12-09 09:07:28
【问题描述】:

你好朋友enter link description here

此链接为我的应用程序创建列表视图小部件,但在这里我想在列表视图下方添加一个按钮,单击该按钮将打开我的应用程序, 使用下面的代码来实现这一点,因为它在其他应用程序上对我有用,但不适用于这个例子,如何做到这一点。

类小部件提供者

public class WidgetProvider extends AppWidgetProvider {

    // String to be sent on Broadcast as soon as Data is Fetched
    // should be included on WidgetProvider manifest intent action
    // to be recognized by this WidgetProvider to receive broadcast
    public static final String DATA_FETCHED = "com.wordpress.laaptu.DATA_FETCHED";


    /*
     * this method is called every 30 mins as specified on widgetinfo.xml this
     * method is also called on every phone reboot from this method nothing is
     * updated right now but instead RetmoteFetchService class is called this
     * service will fetch data,and send broadcast to WidgetProvider this
     * broadcast will be received by WidgetProvider onReceive which in turn
     * updates the widget
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        // which layout to show on widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        Intent informationIntent = new Intent(context,info.class);
        PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.btnSeeMore,infoPendingIntent);

        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            Intent serviceIntent = new Intent(context, RemoteFetchService.class);
            serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetIds[i]);
            context.startService(serviceIntent);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

清单添加了一个活动

<activity android:name=".info" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.INFO" />
            </intent-filter>
        </activity>

当点击它应该打开我的应用程序,如何做到这一点请帮忙,谢谢

最后我解决了我想要得到的东西, 刚刚在接收上写了一个按钮点击代码,它工作了

@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals(DATA_FETCHED)) {
            Log.i("inside action", "DATA_FETCHED");
            int appWidgetId = intent.getIntExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(context);
            RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);

              Intent informationIntent = new Intent(context,MainActivity.class);
              PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
              remoteViews.setOnClickPendingIntent(R.id.txtSeeMore,infoPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }   

}

【问题讨论】:

    标签: android listview android-appwidget


    【解决方案1】:

    您需要通过覆盖您的提供者中的 onReceive 方法来定义您正在设置的 PendingIntent 将如何被处理:

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals(ACTION_NAME)) {
            // Open your activity here.
        }
    }
    

    如果您的 ListView 中的每个项目都需要不同的额外内容来帮助 onReceive 创建正确的 Intent,您可以在 getViewAt 中为 ListView 的每个项目使用 FillInIntent:

    // Set the onClickFillInIntent
    final Intent fillInIntent = new Intent();
    final Bundle extras = new Bundle();
    extras.putInt(YourWidgetProvider.ACTION_EXTRA_ITEM, event.getId()); // retrieve this in onReceive with intent.getIntExtra(ACTION_EXTRA_ITEM, -1)
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.listview_main_layout, fillInIntent);
    return rv;
    

    另外,不要忘记将您的 Provider 和 RemoteViewsService 添加到您的清单中:

    <receiver 
        android:name="com.example.YourWidgetProvider"
        android:label="@string/widget_name">
         <intent-filter>
             <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
         </intent-filter>
         <meta-data
             android:name="android.appwidget.provider"
             android:resource="@xml/provider_definition_in_xml" />
    </receiver>
    
    <service android:name="com.example.YourRemoteViewsService"
         android:permission="android.permission.BIND_REMOTEVIEWS"
         android:exported="false" />
    

    编辑:

    要处理对主窗口小部件(而不是集合视图)中按钮的点击,它甚至更容易:

    rv.setOnClickPendingIntent(R.id.button_id, PendingIntent.getBroadcast(context, 0,
                            new Intent(context, YourWidgetProvider.class)
                            .setAction(ACTION_NAME),
                            PendingIntent.FLAG_UPDATE_CURRENT));
    

    当然,ACTION_NAME 需要与您在集合视图上的点击所使用的名称不同,因为它需要产生不同的 Intent。

    我希望这会有所帮助!

    【讨论】:

    • 如果您需要完整实现的示例,您可以查看docs.huihoo.com/android/3.0/resources/samples/StackWidget/… 或broculos.net/2011/12/… ;)
    • 请你建议我如何在上面提到的链接中得到这个,因为你建议的方法我在其他例子中尝试过,在那里有效,但链接没有,
    • 让我澄清一下,我的小部件上有 1 个按钮,点击它应该打开我的应用程序,而不是在 listview 行点击
    • 查看我编辑的答案。您只需设置您的提供者,而不是将您的服务作为用于意图的类。
    • 我试过 ,12-09 16:01:48.060: I/InputReader(472): 触摸事件的动作是 0x0 (deviceType=0) [pCnt=1, s=0.37788] 12-09 16 :01:48.060:I / InputDispatcher(472):将触摸传递到当前输入目标:动作:0x0 12-09 16:01:48.060:I / InputDispatcher(472):将触摸传递到当前输入目标:动作:0x0 12- 09 16:01:48.080:I/InputDispatcher(472):向当前输入目标传递触摸:动作:0x0 12-09 16:01:48.080:I/InputDispatcher(472):向当前输入目标传递触摸:动作:0x0 12-09 16:01:48.170: I/TwDVFSBroadcastReceiver(770): onLauncherIntent
    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多