widget的用途:
可以添加到手机桌面上的程序
widget的特点:
快捷、方便
个性化、可自定义功能--实时地显示新闻的内容
可及时控制更新widget显示内容
widget用法步骤:
1、绘制widget布局--支持的布局只有imageView、imageButton、textView、progressBar、clock
如果想要使用更复杂的view,就需要对framwork进行改写
2、配置widget的基本属性:
例如添加支持的最大宽度、最小宽度、最大最小高度等
3、定义AppWidgetProvider
类似于android开发中的广播,接收、更新的操作都在这里
4、提供ConfigurationActivity
用来初始化widget,一些配置等
按照上述步骤来写
1、widget的布局:
在res文件夹下的layout文件夹下创建一个名为widget的布局文件,如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clock显示"/>
</LinearLayout></span>
2、配置widget的基本属性:
在res文件夹下创建一个xml文件,并在xml文件夹下新建一个xml resource file文件,root element 改为appwidget-provider
具体内容为:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="80dp"
android:minHeight="40dp"
android:initialLayout="@layout/widget"
android:updatePeriodMillis="8640000">
</appwidget-provider>
<!--创建配置文件-->
<!--android:initialLayout="@layout/widget"是组件的布局-->
<!--android:updatePeriodMillis="8640000"是设置更新的时间间隔,最短为30分钟,小于30分钟也为30分钟-->
3、定义AppWidgetProvider,代码如下
WidgetProvider.java
package com.feng.demo.widget_clock;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
/**
* Created by Administrator on 2016/4/27 0027.
* 类似于广播
*/
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
//widget被从屏幕移除
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
//最后一个widget被从屏幕移除执行
context.stopService(new Intent(context, TimerService.class));
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
//第一个widget添加到屏幕上执行
//启动服务
context.startService(new Intent(context, TimerService.class));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//这个方法一般不被重写,因为无论操作哪个方法,都会调用onReceive
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
//刷新widget执行
//通过remoteView和AppWidgetManager执行更新操作
}
}
在这里启动和关闭服务
4、创建Service,在其中设置widget的显示和更新,代码如下:
package com.feng.demo.widget_clock;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Administrator on 2016/4/27 0027.
* 当把widget添加到桌面时,启动服务,动态更改时间
*/
public class TimerService extends Service {
private Timer timer;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
timer.schedule(new TimerTask() {
/*在这里更新widget这个View*/
@Override
public void run() {
updateViews();
}
},0,1000);//period周期
}
private void updateViews() {
String time = sdf.format(new Date());
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget);
rv.setTextViewText(R.id.tv,time);
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName cn = new ComponentName(getApplicationContext(), WidgetProvider.class);//这里的WidgetProvider是想要通知的类
manager.updateAppWidget(cn,rv);//执行这个方法的时候就可以通知appwidgetprovider去执行刷新操作
//就会去实时去刷新节目更新时间
}
@Override
public void onDestroy() {
super.onDestroy();
timer=null;
}
}
最后,别忘了在清单文件中配置service和receiver
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.feng.demo.widget_clock">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TimerService"/>
<receiver android:name=".WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<!--name必须写成这样-->
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widgetconfig"/>
<!--meta-data里定义配置文件,name必须写成这样-->
</receiver>
</application>
</manifest>
写完之后就可以运行了,效果如下:
其中画红色线条的部分会像时钟一样,秒位上的数值每隔一秒增加1
程序运行之后会先出现一个入口activity对应的布局界面,将界面返回后在小组件中才能找到widget组件,且如果程序不在启动状态时clock不随时间每隔一秒加1