• Android简易实战教程--第十四话《模仿金山助手创建桌面Widget小部件》


    打开谷歌api,对widget小部件做如下说明:

    App Widgets are miniature application views that can be embedded in otherapplications(such as the Home screen) and receive periodic updates. These views arereferred to as Widgets in the user interface,and you can publish one with an App Widget provider. An application componentthat is able to hold other App Widgets is called an App Widget host. The screenshotbelow showsthe Music App Widget.还举例说明了最常用的小部件为音乐播放器widget:

    文档给出了很详细的使用方法。具体的操作如下:

    First, declare the AppWidgetProvider class in yourapplication'sAndroidManifest.xml file. For example:

    <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>

    那么照猫画虎,我们在自己应用创建一个demo来做出这个小部件:

    新建工程,多创建一个类:public class ExampleAppWidgetProvider extends AppWidgetProvider{}它是一个特殊的广播接受者

    把上边代码,全部复制到自己的清单文件,发现android:resource="@xml/example_appwidget_info"会报错,因为工程里没有这个引用,因此创建xml文件夹,再创建example_appwidget_info这个xml文件,里面的具体内容,继续看api文档:

    接下来看到这么一行文字:

    Adding the AppWidgetProviderInfo Metada

    下边紧跟着这样代码:

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:updatePeriodMillis="86400000"
        android:previewImage="@drawable/preview"
        android:initialLayout="@layout/example_appwidget"
        android:configure="com.example.android.ExampleAppWidgetConfigure" 
        android:resizeMode="horizontal|vertical"
        android:widgetCategory="home_screen">
    </appwidget-provider>

    我们需要把它复制到刚刚创建的example_appwidget_info文件里面

    复制过去之后,会发现两行代码报错:

    android:previewImage="@drawable/preview"
        android:initialLayout="@layout/example_appwidget"

    分贝是布局,以及图片。我们工程显然没有这两个资源,因此需要创建这两个资源。而为了模仿金山卫士的哪个小部件,我直接去复制了金山卫士的资源。

    文件名称为:process_widget_provider

    把里面的内容,全部复制到刚才的example_appwidget_info文件里面

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:initialLayout="@layout/process_widget"
        android:minHeight="72.0dip"
        android:minWidth="294.0dip"
        android:updatePeriodMillis="0" />

    当然,还没引入initialLayout,这行代码肯定也是报错。那么我们就u加入这个布局资源。

    金山助手的res的layout文件下有process_widget文件,全部拷贝到自己工程里面去

    这样,上一次的文件没错误,新的文件错误又来了。不要担心,基本都是没有资源报错的,只要引入相应的资源就好了。

    在金山的源代码中,赋值那些报错的资源引用。最终自该好的代码如下:(不需要理解全部,但要知道如何赋值粘贴,知道制作的过程)

    相信都能通过引用资源,排除这些错误。现在可以运行一下了——

    现在可以把它直接拖拽到桌面上显示了。如下:


    这样,这个小部件已经创建完毕了!模仿show就这样完成了。

    那么,怎么样才能在这个小界面上加入数据呢?之前我们还有一个类,public class ExampleAppWidgetProvider extends AppWidgetProvider{}它是一个特殊的广播接受者

    还没有对它做任何的代码逻辑,其实,这个广播接收者就是与widget联系起来的。加入如下方法,一目了然。现在开始在这里面写代码了:

    首先,体验一些widgit的生命周期。覆盖一些方法如下:

    public class ExampleAppWidgetProvider extends AppWidgetProvider {
    
    	@Override
    	public void onDeleted(Context context, int[] appWidgetIds) {
    		// TODO Auto-generated method stub
    		super.onDeleted(context, appWidgetIds);
    		System.out.println("onDeleted");
    	}
    
    	@Override
    	public void onDisabled(Context context) {
    		// TODO Auto-generated method stub
    		super.onDisabled(context);
    		System.out.println("onDisabled");
    	}
    
    	@Override
    	public void onEnabled(Context context) {
    		// TODO Auto-generated method stub
    		super.onEnabled(context);
    		System.out.println("onEnabled");
    	}
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		// TODO Auto-generated method stub
    		super.onReceive(context, intent);
    		System.out.println("onReceive");
    	}
    
    	@Override
    	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    			int[] appWidgetIds) {
    		// TODO Auto-generated method stub
    		super.onUpdate(context, appWidgetManager, appWidgetIds);
    		System.out.println("onUpdate");
    	}
    	
    }
    

    运行,在此创建widget部件。查看一下log输出:

    加入创建两个小控件:

    看到多了两个打印。再创建——

    当然还是执行上边画红线的输出。

    现在删除一个部件,打印输出:

    onDeleted

    onReceive

    继续删除掉最后一个:

    onDeleted

    onReceive

    onDisabled

    onReceive

    可以总结出:onDisabled和onEnabled都仅仅执行一次。那么这两个方法关键方法;一个创建时执行,一个销毁时执行。一些自己的逻辑代码就写在这两个方法里面。

    至于写什么逻辑,要根据自己的需求来定了。这样,widget小部件的生成就讲完了。









  • 相关阅读:
    JavaScript学习笔记(七) 跨域问题
    JavaScript学习笔记(六) 异步问题
    JavaScript学习笔记(五) jQuery
    查看Wii的系统版本信息
    运行你的应用
    创建一个Android工程
    构建你的第一个App
    Android Studio开发环境部署
    酷派D530刷机指引
    酷派D530刷机指引之民间ROM
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299624.html
Copyright © 2020-2023  润新知