Android桌面插件开发笔记

开发桌面插件步骤:定义桌面插件布局(layout) -> 实现 AppWidgetProvider 子类 -> 定义相关资源文件(res/xml/...)实现appwidget-provider节点  -> 在AndroidManifest.xml文件中声明 实现的 AppWidgetProvider 子类

1、定义桌面插件布局(layout)

//res/layout/app_widget_search.xml


    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


            android:id="@+id/b_widget_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:background="@android:drawable/editbox_background"
        android:text="
提示文本"
        android:textSize="
20dp"
        android:textColor="@color/gray" />



2、实现 AppWidgetProvider 子类

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.os.Bundle;
import android.widget.RemoteViews;


public class Appwidget extends AppWidgetProvider {


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
String act = intent.getAction();
if(act.equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_search);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, new Intent(context, SearchActivityV2.class), Intent.FLAG_ACTIVITY_NEW_TASK);
rv.setOnClickPendingIntent(R.id.b_widget_search, pendingIntent);//点击事件,跳转到SearchActivityV2 页面

//将该界面显示到插件中
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context,Appwidget.class);
appWidgetManager.updateAppWidget(componentName, rv);
}
}


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


@Override
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager, int appWidgetId,
Bundle newOptions) {
// TODO Auto-generated method stub
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
newOptions);
}


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


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


@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
}


}


3、定义相关资源文件(res/xml/...)实现appwidget-provider节点

//res/xml/mywidget.xml

    android:minWidth="300dp"
    android:minHeight="60dp"
    android:initialLayout="@layout/app_widget_search" >


4、在AndroidManifest.xml文件中声明 实现的 AppWidgetProvider 子类


           
               
                //必须声明
           

                           android:name="android.appwidget.provider"  
               android:resource="@xml/mywidget" />
       


然后运行程序后返回桌面,长按空白地方选择桌面小部件拖拽到桌面就可以运行了。

你可能感兴趣的:(Android桌面插件开发笔记)