AppWidget简单实现

1.首先写个类继承AppWidgetProvider,可以重写六个方法如下:

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onReceive");
		super.onReceive(context, intent);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		System.out.println("onUpdate");

	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		System.out.println("onDeleted");
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		System.out.println("onEnabled");
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		System.out.println("onDisabled");
		super.onDisabled(context);
	}
2、随便准备一布局文件,比如example_appwidget_info.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:onClick="button"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


3、 在res文件夹下新建一文件夹xml,在xml文件夹下新建一XML文件,比如example_appwidget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/example_appwidget_info">
</appwidget-provider>

4、在AndroidManifest注册一receiver,格式如下
        <receiver android:name="AppWidgetInfo">  
            <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>
5、运行一下软件,退出长按屏幕就能发现添加控件中有自己的控件了
AppWidget简单实现_第1张图片


你可能感兴趣的:(android,xml,layout,button,encoding)