Android6.0添加桌面插件Widget

分析了Widgets源码,那么如何添加自己定义的widgets插件呢?先把效果图附上

Android6.0添加桌面插件Widget_第1张图片

一、步骤:

1、设计组件UI:widget_music.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:textColor="#ffffff"
        android:textSize="18dp"
        android:gravity="center"
        android:text="@string/music_widget"
        android:background="#00E3E3"
    />

LinearLayout>

2、设置UI容器:appwidget_music_info.xml

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

3、写个类继承AppWidgetProvider(MusicWidgetProvider.java)

package com.android.music;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.view.View;
import android.widget.RemoteViews;
import com.android.music.R;

public class MusicWidgetProvider extends AppWidgetProvider{

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // Update each requested appWidgetId
        RemoteViews view = buildUpdate(context);

        for (int i = 0; i < appWidgetIds.length; i++) {
            appWidgetManager.updateAppWidget(appWidgetIds[i], view);
        }
    }

    static RemoteViews buildUpdate(Context context) {
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.music_widget);

        Intent intent = new Intent(context,MusicBrowserActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                    0 /* no requestCode */, intent, 0 /* no flags */);
        views.setOnClickPendingIntent(R.id.button, pendingIntent);
        return views;
    }   
    public void onEnabled(Context context) {
        //super.onEnabled();
    }

    public void onDisabled(Context context) {
        //super.onDisabled();
    }
}

4、在Manifest.xml中注册receiver;


<receiver android:name="com.android.music.MusicWidgetProvider">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        <action android:name="android.appwidget.action.APPWIDGET_DISABLED"/>
        <action android:name="android.appwidget.action.APPWIDGET_ENABLED"/>
    intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_music_info" />
receiver>

二、编译结束后遇到的bug:把widget插件拖到桌面的时候组件没法正常显示,提示Problem loading widget。

Android6.0添加桌面插件Widget_第2张图片

打印的日志如下:

01-01 08:08:25.607 1802-1802/? W/AppWidgetHostView: updateAppWidget couldn't find any view, using error viewandroid.view.InflateException: Binary XML file line #8: Binary XML file line #8: You must supply a layout_width attribute.
                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                        at android.widget.RemoteViews.apply(RemoteViews.java:2762)
                                                        at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:393)
                                                        at com.android.launcher3.LauncherAppWidgetHostView.updateAppWidget(LauncherAppWidgetHostView.java:69)
                                                        at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:349)
                                                        at com.android.launcher3.LauncherAppWidgetHost.createView(LauncherAppWidgetHost.java:118)
                                                        at com.android.launcher3.widget.WidgetHostViewLoader$2.run(WidgetHostViewLoader.java:106)
                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                        at android.os.Looper.loop(Looper.java:148)
                                                        at android.app.ActivityThread.main(ActivityThread.java:5452)
                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
                                                     Caused by: java.lang.UnsupportedOperationException: Binary XML file line #8: You must supply a layout_width attribute.
                                                        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:709)
                                                        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6923)
                                                        at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:7105)
                                                        at android.widget.LinearLayout$LayoutParams.(LinearLayout.java:1911)
                                                        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1816)
                                                        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1815)
                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                        at android.widget.RemoteViews.apply(RemoteViews.java:2762) 
                                                        at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:393) 
                                                        at com.android.launcher3.LauncherAppWidgetHostView.updateAppWidget(LauncherAppWidgetHostView.java:69) 
                                                        at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:349) 
                                                        at com.android.launcher3.LauncherAppWidgetHost.createView(LauncherAppWidgetHost.java:118) 
                                                        at com.android.launcher3.widget.WidgetHostViewLoader$2.run(WidgetHostViewLoader.java:106) 
                                                        at android.os.Handler.handleCallback(Handler.java:739) 
                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                        at android.os.Looper.loop(Looper.java:148) 
                                                        at android.app.ActivityThread.main(ActivityThread.java:5452) 
                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762) 
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 


                                                    [ 01-01 08:08:26.195  4790: 4790 E/         ]
                                                    memsic Open mxc400x device failed! fd_acc = -1
01-01 08:08:26.566 1141-1469/? W/BackupManagerService: dataChanged but no participant pkg='com.android.launcher3' uid=10032
01-01 08:08:26.859 1141-4333/? W/BackupManagerService: dataChanged but no participant pkg='com.android.launcher3' uid=10032

哈哈,犯了个低级错误,没有定义layout_width属性,导致引入的xml出错,修改之后就可以正常显示了

你可能感兴趣的:(Android6.0添加桌面插件Widget)