Android之App Widgets开发初步
来源:互联网 发布:独立游戏开发入门 知乎 编辑:程序博客网 时间:2024/06/06 03:44
AppWidget就是我们平常在桌面上见到的那种一个个的小窗口,利用这个小窗口可以给用户提供一些方便快捷的操作。下面让我们在桌面 上实现一个简单的AppWidget。然后为App Widget指定样式和布局:myappwidget.xml 和我们平时的布局文件一样,可以设置自己喜欢的类型。 接下来实现继承AppWidgetProvider的类:ExampleAppWidgetProvider其实AppWidgetProvider就是继承了BroadcastReceiver,可以看成是一个特殊的BroadcastReceiver。它里面有两个重要的方法onReceive()onUpdate()
首先定义AppWidgetProviderInfo:在res/xml文件夹中定义一个名为 :myappwidgetprovider.xml。
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="72dp"
- android:minHeight="72dp"
- android:updatePeriodMillis="86400000"
- android:initialLayout="@layout/myappwidget">
- </appwidget-provider>
- <?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:background="#ffffff"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button" />
- </LinearLayout>
- public class ExampleAppWidgetProvider extends AppWidgetProvider {
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- // TODO Auto-generated method stub
- final int N = appWidgetIds.length;
- for (int i=0; i<N; i++) {
- int appWidgetId = appWidgetIds[i];
- Intent intent = new Intent(context, SimpleDemoActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
- RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myappwidget);
- views.setOnClickPendingIntent(R.id.button, pendingIntent);
- // Tell the AppWidgetManager to perform an update on the current app widget
- appWidgetManager.updateAppWidget(appWidgetId, views);
- }
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- super.onReceive(context, intent);
- }
- }
最后在manifest中加入:
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:label="@string/app_name"
- android:name=".SimpleDemoActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name="ExampleAppWidgetProvider" >
- <intent-filter >
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- <meta-data
- android:name="android.appwidget.provider"
- android:resource="@xml/myappwidgetprovider" />
- </receiver>
- </application>
这样就完成了一个简单了Widgets,运行这个工程。
- android之App Widgets开发初步
- Android之App Widgets开发初步
- Android之App Widgets开发初步
- android开发步步为营之14:App Widgets
- Android组件之App Widgets
- 【android开发】桌面小挂件( APP Widgets )
- Android App Widgets
- android App Widgets
- Android-App Widgets
- Android App widgets
- Android Developer - App Widgets
- Android API Guides 之 App Components(16) - App Widgets
- Android:Android开发指南-窗口小部件(App Widgets)
- Android开发指南-窗口小部件(App Widgets)
- Android开发指南-窗口小部件(App Widgets)
- Android开发指南-窗口小部件(App Widgets)
- Android开发指南-窗口小部件(App Widgets)
- Android开发指南-窗口小部件(App Widgets)
- Mysql 常用操作
- ActionScript3语法 ----(1)
- Android英文名词解释大全(持续更新中......)
- 信息系统资源管理
- AudioManager音量控制
- Android之App Widgets开发初步
- 释放SQL Server占用的内存
- performSelectorOnMainThread (线程)
- CSS filter滤镜的用法
- 发现并防止托管代码中出现内存泄漏,C# 内存泄漏,.net 内存泄漏
- Android adb shell 命令详解
- 国家气象局提供的天气预报接口(完整Json接口)
- DLL中GDIPlus初始化和退出时注意的问题
- Java I/O总结——Reader