【问题标题】:How to declare a widget properly in the manifest?如何在清单中正确声明小部件?
【发布时间】:2014-05-08 23:44:29
【问题描述】:

大家好,我对小部件完全陌生,我不知道如何在清单中声明小部件,每当我尝试时都会出错

小部件正在与已在清单中定义的服务通信

包名 包源.justanothermusicplayer.service;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        Intent playIntent = new Intent(Player.BROADCAST_PLAYPAUSE);//player is a class which starts the service 


        PendingIntent playPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, playIntent, INTENT_FLAGS);

        controlButtons.setOnClickPendingIntent(
                R.id.bPlay, playPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         
    }
}

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    receive Widget Update 的清单应如下所示

        <receiver android:name="com.example.app.provider.CustomAppWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
    
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
    

    Widget Provider 资源 xml 应该看起来像这样

    <appwidget-provider
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:minWidth="200dp"
       android:updatePeriodMillis="0"
       android:minHeight="100dp"
       android:initialLayout="@layout/initial_layout">
    

    【讨论】:

    • android: name 不会是我的班级?我的意思是在我的情况下它不会是“source.justanothermusicplayer.service.MyWidget”吗?或者它会是你写它的方式吗?
    • 没有。它应该是您的自定义 AppWidgetProvider 类名。我只写了一个示例
    【解决方案2】:

    以下代码来自官方文档。

    <receiver android:name="ExampleAppWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/example_appwidget_info" />
    </receiver>
    

    有关详细信息,请参阅AppWidget Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多