【Android Widget】开发
程序员文章站
2022-05-30 17:45:48
...
Android Widget开发
一.开发步骤
- 1.绘制Widget布局(比较局限性的View)
- 2.配置Widget的属性
- 3.自定义APPwidgetProvicer
- 4.提供Configuration Activity(配置Activity)
二.实现桌面时钟
需求:
- 1.实时更新时间
- 2.添加,删除
1.appwidget-provider
既然我们要使用它,我们就先在res目录下新建一个xml文件夹,然后再new一个appwidget-provider,再新建一个layout名叫widget_clock.xml等下我们调用
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_clock"
android:minHeight="40dp"
android:minWidth="100dp"
android:updatePeriodMillis="8640000" >
</appwidget-provider>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
再新建一个layout,名为widget_clock.xml就是我们widget小组件的布局了,因为我们只要一个时钟,所以,就只写个TextView就OK了,ID是tv_clock;
2.ClockProvider
主要是监听小组件的状态
package com.lgl.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
/**
* 时钟小组件的监听
*
* @author lgl
*
*/
public class ClockProvider extends AppWidgetProvider {
// 刷新的时候执行
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
// 第一个添加到屏幕上
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
// 启动Service
context.startService(new Intent(context, ClockService.class));
}
// 最后一个widget从屏幕移除
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
context.stopService(new Intent(context, ClockService.class));
}
// 从屏幕移除
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
3.ClockService
当我们把小组件添加到我们的屏幕上的时候,我们就启动这个服务去更新时间
package com.lgl.widget;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
/**
* 更新小组件事件的服务
*
* @author lgl
*
*/
public class ClockService extends Service {
// 定时器
private Timer timer;
// 日期格式
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new Timer();
/**
* 参数:1.事件2.延时事件3.执行间隔事件
*/
timer.schedule(new TimerTask() {
@Override
public void run() {
updateView();
}
}, 0, 1000);
}
/**
* 更新事件的方法
*/
private void updateView() {
// 时间
String time = sdf.format(new Date());
/**
* 参数:1.包名2.小组件布局
*/
RemoteViews rViews = new RemoteViews(getPackageName(),
R.layout.widget_clock);
// 显示当前事件
rViews.setTextViewText(R.id.tv_clock, time);
// 刷新
AppWidgetManager manager = AppWidgetManager
.getInstance(getApplicationContext());
ComponentName cName = new ComponentName(getApplicationContext(),
ClockProvider.class);
manager.updateAppWidget(cName, rViews);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer = null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
我们的服务写好了之后我们就可以去监听了,当我们创建了小组件的时候就启动服务,当我们移除了小组件的时候就停止服务
// 第一个添加到屏幕上
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
//启动Service
context.startService(new Intent(context,ClockService.class));
}
// 最后一个widget从屏幕移除
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
context.stopService(new Intent(context,ClockService.class));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
但是别忘了,我们还需要在AndroidMainifest.xml中注册一下
<receiver android:name="com.lgl.widget.ClockProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetconfig" />
</receiver>
<service android:name="com.lgl.widget.ClockService" >
</service>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
好了运行一下
上一篇: Webpack4打包vue项目
下一篇: webpack 4打包