android使用AppWidget
2013-03-30 20:05
288 查看
AppWidget简单理解就是应用程序的手机桌面快捷方式,也就是说点快捷方式和点菜单里的图标效果是一样的,通过这种机制可以打开应用程序。
AppWidget是BroadCast的一个子类,因此这个类和广播的操作是类似的,下面就一步一步来弄下这个吧
1.编写该组件对于桌面图标的快捷方式,一般来说就是一个图标
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mldn_3g"
android:id="@+id/img"
/>
</LinearLayout>
下面还要配置一个xml用来控制该组件的属性如高度、宽度、更新时间等
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="80px"
android:minWidth="300px"
android:updatePeriodMillis="6000"
android:initialLayout="@layout/appwidget">
</appwidget-provider>
还有最后一个配置文件,因为该组件也运用到了广播机制所以要在AndroidManifest.xml里增加如下配置
<receiver android:name=".MyAppwidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<!-- <intent-filter>
<action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" />
</intent-filter> -->
<!--appwidget是控制组件属性的那个xml -->
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget" />
</receiver>
2.编写AppWidgetProvider,只需要写一个类继承AppWidgetProvider就行,我们只需要覆写onUpdate方法即可
package org.lxh.activity;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
public class MyAppwidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for(int i=0;i<appWidgetIds.length;i++){
//设置activity
Intent intent=new Intent(context,AppwidgetActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//定义要操作的RemoteViews
RemoteViews remote=new RemoteViews(context.getPackageName(),R.layout.appwidget);
remote.setOnClickPendingIntent(R.id.img, pIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remote);
}
}
}
到这里程序就写完了,除了这种方法外还有一种方法可以实现相同的操作代码如下
package org.lxh.demo;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyAppWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if ("org.lxh.action.MYAPPWIDGET_UPDATE".equals(intent.getAction())) {
RemoteViews remote = new RemoteViews(context.getPackageName(),
R.layout.mldn_appwidget);
remote.setImageViewResource(R.id.img, R.drawable.mldn_man);
remote.setTextViewText(R.id.but, "www.MLDNJAVA.cn") ;
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
ComponentName componentName = new ComponentName(context,
MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remote) ;
} else {
super.onReceive(context, intent) ; // 如果不写此代码,表示无法调用onUpdate()
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent intent = new Intent();
intent.setAction("org.lxh.action.MYAPPWIDGET_UPDATE") ;
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remote = new RemoteViews(context.getPackageName(),
R.layout.mldn_appwidget);
remote.setOnClickPendingIntent(R.id.but, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remote);
}
}
这种实现对于的配置文件如下
AppWidget是BroadCast的一个子类,因此这个类和广播的操作是类似的,下面就一步一步来弄下这个吧
1.编写该组件对于桌面图标的快捷方式,一般来说就是一个图标
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mldn_3g"
android:id="@+id/img"
/>
</LinearLayout>
下面还要配置一个xml用来控制该组件的属性如高度、宽度、更新时间等
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="80px"
android:minWidth="300px"
android:updatePeriodMillis="6000"
android:initialLayout="@layout/appwidget">
</appwidget-provider>
还有最后一个配置文件,因为该组件也运用到了广播机制所以要在AndroidManifest.xml里增加如下配置
<receiver android:name=".MyAppwidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<!-- <intent-filter>
<action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" />
</intent-filter> -->
<!--appwidget是控制组件属性的那个xml -->
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget" />
</receiver>
2.编写AppWidgetProvider,只需要写一个类继承AppWidgetProvider就行,我们只需要覆写onUpdate方法即可
package org.lxh.activity;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
public class MyAppwidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for(int i=0;i<appWidgetIds.length;i++){
//设置activity
Intent intent=new Intent(context,AppwidgetActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//定义要操作的RemoteViews
RemoteViews remote=new RemoteViews(context.getPackageName(),R.layout.appwidget);
remote.setOnClickPendingIntent(R.id.img, pIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remote);
}
}
}
到这里程序就写完了,除了这种方法外还有一种方法可以实现相同的操作代码如下
package org.lxh.demo;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyAppWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if ("org.lxh.action.MYAPPWIDGET_UPDATE".equals(intent.getAction())) {
RemoteViews remote = new RemoteViews(context.getPackageName(),
R.layout.mldn_appwidget);
remote.setImageViewResource(R.id.img, R.drawable.mldn_man);
remote.setTextViewText(R.id.but, "www.MLDNJAVA.cn") ;
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
ComponentName componentName = new ComponentName(context,
MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remote) ;
} else {
super.onReceive(context, intent) ; // 如果不写此代码,表示无法调用onUpdate()
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent intent = new Intent();
intent.setAction("org.lxh.action.MYAPPWIDGET_UPDATE") ;
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remote = new RemoteViews(context.getPackageName(),
R.layout.mldn_appwidget);
remote.setOnClickPendingIntent(R.id.but, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remote);
}
}
这种实现对于的配置文件如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyAppWidgetDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyAppwidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget" /> </receiver> </application> </manifest>
相关文章推荐
- ECLIPSE使用appcompat_v7库无法找到android:Widget.Material.ActionButton的问题
- android 开发之App Widget 使用
- 解决 android.support.v7.widget.GridLayout 使用 xmlns:app 出现 error 的问题
- Android学习二_七:AppWidget使用(三) (2011-12-31 23:09:17)转载▼
- Android学习--AppWidget的使用
- Android的桌面组件App Widget的使用方法 AppWidgetProvider RemoteViews
- Android App Widget的简单使用
- ECLIPSE使用appcompat_v7库无法找到android:Widget.Material.ActionButton的问题
- Android笔记之AppWidget使用
- android中Appwidget的使用
- Android 桌面小组件AppWidget的使用
- android桌面小部件appwidget使用ListView或者StackView如何刷新
- android app widget,安卓桌面小工具的使用
- Android开发历程_15(AppWidget的使用)
- android AppWidget的使用以及利用Service TimerTask实现widget的定时更新
- Android---快速拨话程序(涵盖appWidget、Tab、联系人、多线程、自定义ListView使用)
- Android---快速拨话程序(涵盖appWidget、Tab、联系人、多线程、自定义ListView使用)
- Android入门——AppWidget及Broadcast的使用
- android 桌面组件 App widget的使用
- android典型代码系列(二十六)------App widget的使用