android 创建桌面小控件
2016-05-21 00:26
495 查看
今天我们来谈谈如何创建桌面小控,我们的Android机的桌面上那些时间,日期都属于桌面控件。
// 首先新建一个MyWidgest .java文件 extends AppWidgetProvider
//AppWidgetProvider 的生命周期分别有如下
二、在 AndroidManifest.xml 里配置 该定义的类
三、在res下创建文件夹 xml 新建一个.xml文件
@xml/example_appwidget_info
就这样,你可以运行项目查看测试结果了,长按桌面–》添加小工具–》然后找到本项目的就好了。可能各个版本步骤不同,大家点点看哈。
下面我用一个清理正在运行的软件的例子跟大家详细解说:下面我先给布局文件process_widget.xml 大家
新建一个服务,定时刷新当前正在进行的软件个数和内存 UpdataWidgetService
在清单文件里配置
在清单文件里配置
UpdataWidgetService 代码 (可能略有复杂,想简单的只参考参考,自己去弄个简单的):
}
在MyWidgest 的onEnabled里开启服务
//开启服务,定期更新widget
创建一个按钮清理完成后的广播接受者
}
在清单文件里配置
一个略复杂的桌面控件就完成了。
// 首先新建一个MyWidgest .java文件 extends AppWidgetProvider
//AppWidgetProvider 的生命周期分别有如下
//第一次执行只会创建一次 @Override public void onEnabled(Context context) { //一般开启服务,定期更新widget 调用该方法 super.onEnabled(context); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); } //控件被移除执行的方法 @Override public void onDisabled(Context context) { //停止服务,和一些销毁处理 } @Override public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) { super.onRestored(context, oldWidgetIds, newWidgetIds); }
二、在 AndroidManifest.xml 里配置 该定义的类
<receiver android:name="com.example.weiget.MyWedget" > <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name="android.appwidget.provider" // 该参数详情见下面 android:resource="@xml/example_appwidget_info" /> </receiver>
三、在res下创建文件夹 xml 新建一个.xml文件
@xml/example_appwidget_info
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="200dp" android:minHeight="50dp" android:updatePeriodMillis="86400000" <!--该布局文件为显示在桌面的文件 源代码下面--> android:initialLayout="@layout/process_widget.xml"> </appwidget-provider>
就这样,你可以运行项目查看测试结果了,长按桌面–》添加小工具–》然后找到本项目的就好了。可能各个版本步骤不同,大家点点看哈。
下面我用一个清理正在运行的软件的例子跟大家详细解说:下面我先给布局文件process_widget.xml 大家
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/widget_bg_portrait" android:gravity="center_vertical" > <LinearLayout android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_marginLeft="5.0dip" android:layout_weight="1.0" android:background="@drawable/widget_bg_portrait_child" android:gravity="center_vertical" android:orientation="vertical" android:paddingBottom="3.0dip" android:paddingTop="3.0dip" > <TextView android:text="正在运行软件:14" android:id="@+id/process_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10.0dip" android:textAppearance="@style/widget_text" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="1.0dip" android:layout_marginTop="1.0dip" android:background="@drawable/widget_bg_portrait_child_divider" /> <TextView android:text="可用内存:411.25MB" android:id="@+id/process_memory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10.0dip" android:textAppearance="@style/widget_text" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" > <ImageView android:layout_width="20.0dip" android:layout_height="20.0dip" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/textColorPrimary" /> </LinearLayout> <Button android:id="@+id/btn_clear" android:layout_width="90.0dip" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginTop="5.0dip" !--这行资源自给去找-> ndroid:background="@drawable/function_greenbutton_selector" android:text="一键清理" android:textColor="#ffffff" /> </LinearLayout> </LinearLayout>
新建一个服务,定时刷新当前正在进行的软件个数和内存 UpdataWidgetService
在清单文件里配置
在清单文件里配置
<service android:name="com.qzzhu.mobileguard.service.UpdataWidgetService" > </service>
UpdataWidgetService 代码 (可能略有复杂,想简单的只参考参考,自己去弄个简单的):
public class UpdataWidgetService extends Service { private Timer timer; private TimerTask task; private AppWidgetManager awm; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { awm = AppWidgetManager.getInstance(this); timer = new Timer(); task = new TimerTask() { @Override public void run() { ComponentName provider=new ComponentName(getApplicationContext(), MyWidget.class); RemoteViews views =new RemoteViews(getPackageName(), R.layout.process_widget); views.setTextViewText(R.id.process_count,"正在运行的软件:"+TaskInfoParser.getRunningTaskInfo(getApplicationContext()).size()); String avalisize = Formatter.formatFileSize(getApplicationContext(), SystemInfoUtils.getAvailMem(getApplicationContext())); views.setTextViewText(R.id.process_memory, "可用内存"+avalisize); Intent intent = new Intent(); intent.setAction("com.qzzhu.killTask"); PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent); awm.updateAppWidget(provider, views); } }; timer.schedule(task, 0,5000); super.onCreate(); } @Override public void onDestroy() { timer.cancel(); task.cancel(); timer=null; task=null; super.onDestroy(); }
}
在MyWidgest 的onEnabled里开启服务
//开启服务,定期更新widget
Intent intent = new Intent(context,UpdataWidgetService.class);
context.startService(intent);
创建一个按钮清理完成后的广播接受者
public class KillTaskReciver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); for (RunningAppProcessInfo info :am.getRunningAppProcesses()) { am.killBackgroundProcesses(info.processName); } Toast.makeText(context, "清理完毕!!", 0).show(); }
}
在清单文件里配置
<receiver android:name="com.qzzhu.mobileguard.receiver.KillTaskReciver" > <intent-filter> <action android:name="com.qzzhu.killTask" /> </intent-filter> </receiver>
一个略复杂的桌面控件就完成了。
相关文章推荐
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- android评星控件RatingBar
- android开发笔记之权重(layout_weight)
- 深入JVM系列之内存模型与内存分配及 AndroidGC原理
- Android Studio 首次安装报错 Java.lang.RuntimeException:java.lang.NullPointerException...错
- Android studio NDK配置使用
- android 中怎么控制EditText只能输入数字和字母?
- Android-->如何填MediaRecorder的坑(附TextureView的使用)
- Android权限系统
- android中事件分发机制
- Android官方文档之Creating a Content Provider
- android随笔小记ContentProvider
- 用SurfaceView绘制正弦曲线
- android讲义 2016/5/8
- android判空
- Android源码解析(二十四)-->onSaveInstanceState执行时机
- android studio 常用插件汇总
- greenDao简单的增删改查
- VirtualBox安装android-x86虚拟机