android studio创建桌面插件widget
2016-01-29 10:18
519 查看
之前看别人说用android studio ,而我还是用着ecilipse不以为然,试了一次,感觉还不如ecilipse好用,后来还是一直用的ecilipse,知道最近用起了android studio才发现真的很强大,支持各种功能,扯远了,在ecilipse中制作桌面插件,又要配置这个又要配置那个,而在android studio中真的是一键搞定,没有比这个更好的了。下面来看看android studio生成的一些关于AppWidgets的一些文件。我们来按照一般的设计流程看下代码:1.新建AppWidgetProvider继承类:MyAppWidget
public class MyAppWidget extends AppWidgetProvider { //第一次加载时调用,是个静态函数,很明显 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // 加载布局文件生成RemoteViews,即要显示的View RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } //桌面插件内容更新函数,生命周期内,每隔固定时间更新一次,调用一次 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } }2.需要给MyAppWidget弄个布局用来显示:my_app_widget,这个就不用说多少了<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#09C" android:padding="@dimen/widget_margin"> <TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="8dp" android:background="#09C" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic" /> </RelativeLayout>
3.搞个Widget配置文件,描述widget的一些参数:my_app_widget_info
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/my_app_widget" //锁屏插件布局位置 android:initialLayout="@layout/my_app_widget" //加载布局位置 android:minHeight="40dp" android:minWidth="250dp" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="86400000" //更新时间间隔 android:widgetCategory="home_screen|keyguard"></appwidget-provider> //插件模式:桌面和锁屏
4.最后一步也是很重要的一步就是配置文件的设置:AndroidManifest.xml
只加了以下代码:
<receiver android:name=".MyAppWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_app_widget_info" /> </receiver>可以看出,widget其实就是用了广播组件,底层功能还要看底层源代码学习,加油~
相关文章推荐
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- AndroidStudio快捷键汇总
- Android项目中引用本地aar文件的方法
- Android使用BroadcastReceiver实现手机开机之后显示画面的功能
- Android 第四天(上午)
- android中双向滑动listview
- Android开发笔记-Android Studio中安装Genymotion插件解决方案
- Android逆向笔记(1)---工具篇之apktool
- Android开发笔记-最快Android模拟器Genymotion的安装与使用完整教程(多图,慎点)
- Android中ProgressBar用法简单实例
- Xamarin开发Android之所需工具的安装
- android 自定义控件之一
- Android 点击文字实现跳转
- Android Studio目录结构浅析
- Android颜色对应的xml色值
- 将Eclipse代码导入到AndroidStudio的两种方式
- Remote Displayer for Android V1.2
- Android控件的隐藏与显示
- Android 中文件类型与MIME的匹配表
- android-Designing Effective Navigation,Planning Screens and Their Relationships