AppWidgetProvider简介: 提供的可在桌面显示的插件,例如酷狗音乐的播放条(暂停,开始) 编写步骤 : 1.为AppWidget提供一个元文件布局AppWidgetProviderInfo,用来显示widget的界面,2.创建一个WidgetProvider继承自AppWidgetProvider;<3.为WidgetProvider创建一个布局文件也可以直接用main.xml;4.注册Manifest.xml 这个很重要一会下面会讲到。代码部分:
</pre><pre name="code" class="java"> <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="100dp"
android:minHeight="40dp"
//设置更新时间 毫秒单位
android:updatePeriodMillis="86400000"
//引用的布局文件
android:initialLayout="@layout/widget_layout" >
</appwidget-provider> <span style="font-family: Arial, Helvetica, sans-serif;">public class WidgetProvider extends AppWidgetProvider{ </span> //没接收一次广播消息就调用一次,使用频繁
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("recrive");
super.onReceive(context, intent);
}
//每次更新都调用一次该方法,使用频繁
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("update--->");
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
//没删除一个就调用一次
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("Deleted");
super.onDeleted(context, appWidgetIds);
}
//当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("OnEnable");
super.onEnabled(context);
}
//当最后一个该Widget删除是调用该方法,注意是最后一个
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("onDisable");
super.onDisabled(context);
}
} 3)为Widget创建一个显示用的布局文件
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/widget_body"
android:padding="5dp"
>
<TextView
android:id="@+id/widget_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:text="目前有以下单词哦:"
android:paddingBottom="5dp"
/>
<TextView
android:id="@+id/widget_word"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:textStyle="bold|italic"
/>
<TextView
android:id="@+id/widget_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#000000"
android:text="点击查看更多..."
android:paddingTop="2dp"
/>
</LinearLayout>4) AndroidManifest.xml
<receiver android:name=".WidgetProvider">
<meta-data android:name="android.appwidget.provider" //Android系统定义的格式,不能更改,不能打错
android:resource="@xml/appwidget_info"></meta-data> //引用的在res/xml下创建的文件
<intent-filter> //这个是接收广播时的过滤器 Android定义的
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
</receiver> <receiver android:label="@string/app_name" android:name=".WordWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<meta-data
android:resource="@xml/my_widget_provider"
android:name="android.appwidget.provider">
</meta-data>
</receiver>
6364

被折叠的 条评论
为什么被折叠?



