转载请注明本文出自Cym的博客(http://blog.youkuaiyun.com/cym492224103),谢谢支持! AppWidget  通过查找android文档  我们可以知道需要在 AndronManifest.xml 配置 <receiver android:name="ExampleAppWidgetProvider">
<intent-filter >
<actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver> 通过上面的配置参数我们看到需要配置 Java类 ExampleAppWidgetProvider 这个类里面如果不需要功能可以不写代码,但是必须要有该类,因为配置文件需要 该类的生命周期是 使用图标 1 onEnabled onUpdate 删除图标 onDeleted onDisabled 2 当使用了图标再次使用图标 onUpdate 3 删除一个图标,桌面还有图标 onDeleted 把桌面所有该图标删除完毕后才会调用 onDisabled public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
publicvoid onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
Log.i("i", "********onEnable()*");
}
@Override
publicvoid onUpdate(Context context, AppWidgetManager appWidgetManager,
int[]appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager,appWidgetIds);
Log.i("i", "********onUpdate()*");
}
@Override
publicvoid onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Log.i("i", "********onDeleted()*");
}
@Override
publicvoid onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
Log.i("i", "********onDisabled()*");
}
}
配置xml文件夹下面 <?xml version="1.0"encoding="utf-8"?>
<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:initialLayout="@layout/example_appwidget"
>
</appwidget-provider>
通过上面配置文件我们需要引用一个布局文件layout <?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical"
android:background="#f00">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="22sp"
android:text="我是widget"/>
</LinearLayout>
这样就可以在桌面上添加一个小工具 显示时间的小工具 AndroidManifest.xml <activity android:name=".MainActivity"></activity>
<receiver android:name=".TimeWidget">
<intent-filter >
<actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
<service
android:name=".TimeService"></service> TimeWidget.java: public class TimeWidget extendsAppWidgetProvider {
@Override
publicvoid onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
//开启服务
Intent intent =new Intent(context,TimeService.class);
context.startService(intent);
}
@Override
publicvoid onUpdate(Context context, AppWidgetManager appWidgetManager,
int[]appWidgetIds) {
super.onUpdate(context, appWidgetManager,appWidgetIds);
}
@Override
publicvoid onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
@Override
publicvoid onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
//停止服务
Intent intent =new Intent(context,TimeService.class);
context.stopService(intent);
}
}
TimeService.java public class TimeService extends Service {
//定时器
privateTimer timer;
//任务 timerTask
privateTimerTask task = new TimerTask() {
@Override
public void run() {
//开启定时器不断的刷新Widget刷新时间
Datedate = new Date();
date.setTime(System.currentTimeMillis());
// Stringdatestr = DateFormat.getDateFormat(getApplicationContext()).format(date);
// Stringtimestr = DateFormat.getTimeFormat(getApplicationContext()).format(date);
// Stringstr = DateFormat.getLongDateFormat(getApplicationContext()).format(date);
//
// Stringtime = datestr + " " + timestr;
SimpleDateFormatformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Stringtext = format.format(date);
//得到AppWidgetManager
AppWidgetManagerappWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentNameprovider = new ComponentName(getApplicationContext(), TimeWidget.class);
// 得到显示视图
RemoteViewsviews = new RemoteViews(getPackageName(), R.layout.example_appwidget);
// 从显示视图中拿到显示时间的文本进行赋值
views.setTextViewText(R.id.tv_time,text);
// 写意图,做为显示控件的点击触发事件
Intentintent = new Intent(getApplicationContext(),MainActivity.class);
// 设置延迟意图
PendingIntentpendingIntent = PendingIntent.getActivity(
getApplicationContext(),
100,
intent,
0);
// 给显示控件指定意图(点击事件)
views.setOnClickPendingIntent(R.id.tv_time,pendingIntent);
//修改显示视图
appWidgetManager.updateAppWidget(provider,views);
}
};
@Override
publicvoid onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new Timer();
//开始任务
timer.schedule(task, 0, 500);
}
@Override
publicvoid onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
publicIBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
publicvoid onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//停止任务
timer.cancel();
}
}
Xml: <?xml version="1.0"encoding="utf-8"?>
<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:initialLayout="@layout/example_appwidget"
>
</appwidget-provider> Layout: <?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical"
android:background="#f00">
<TextView
android:id="@+id/tv_time"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#fff"
android:textSize="22sp"
android:gravity="center"
android:text="时间"/>
</LinearLayout> 快捷图标   创建快捷图标 /**
* 快捷图标的创建:给Launcher应用里面发送广播接受者,发送一个广播
* 快捷图标里面需要什么内容
* 1 图片
* 2 文字
* 3 意图
*
*
*/
public void create(View v){
if(isExst()){
Toast.makeText(this, "图标存在", 300).show();
}else{
// 发送广播
Intent intent = new Intent();
// 文字
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷图标");
// 图片
Parcelableicon = ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// 意图
Intenti = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setComponent(new ComponentName(this,MainActivity.class));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
// 指定广播动作
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// 发送广播
sendBroadcast(intent);
}
}
private boolean isExst(){
boolean flag = false;
// 查询provider
Uriuri = null;
int sdkVersion =getSdkVersion();
// 解决兼容性问题
if(sdkVersion>=8){
uri =
Uri.parse("content://com.android.launcher2.settings/favorites");
}else{
uri =
Uri.parse("content://com.android.launcher.settings/favorites");
}
Stringselection = "title = ?";
String[]selectionArgs = new String[]{"快捷图标"};
Cursorc = getContentResolver().query(uri, null, selection, selectionArgs,null);
if(c.moveToFirst()){
flag = true;
}
c.close();
return flag;
}
// 得到版本号
private int getSdkVersion() {
// TODO Auto-generated method stub
return android.os.Build.VERSION.SDK_INT;
}
删除快捷图标 public void delete(View v){
// 发送广播
Intent intent = new Intent();
// 文字
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷图标");
// 图片
Parcelableicon = ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// 意图
Intenti = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setComponent(new ComponentName(this,MainActivity.class));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
// 指定广播动作
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
// 发送广播
sendBroadcast(intent);
}
课后问题 怎么去添加一个widget 长按,或者添加 widget有什么样的作用 方便快捷的使用软件功能 创建一个widget需要哪些步骤 配置androidManifest 写xml 写layout 写java类 widget的生命周期 1: onEnabled onUpdate 删除图标 onDeleted onDisabled 2: 当使用了图标再次使用图标 onUpdate 3: 删除一个图标,桌面还有图标 onDeleted 把桌面所有该图标删除完毕后才会调用 onDisabled 快捷图标的数据存在于哪里 数据库 快捷图标的创建原理 发送广播给android系统它接受到你的发送的广播就会根据你传入的参数 生成相应的快捷图标
|