桌面组件AppWidget
来源:互联网 发布:淘宝返利程序 编辑:程序博客网 时间:2024/05/16 08:57
可以添加的窗口小部件,代码
public class MyAppWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
//处理广播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
//更新时触发
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
在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_appwidget"/>
</receiver>
//桌面组件大小设置的xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="300px"
android:minHeight="80px"
android:updatePeriodMillis="3600"
android:initialLayout="@layout/app_provider">
</appwidget-provider>
//在桌面上要显示的组件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/app_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/app_weather"
android:layout_below="@+id/app_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/app_area"
android:layout_below="@+id/app_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="area"
/>
</RelativeLayout>
//要实现在桌面点击的效果,这样可以跳转到指定的Activity,这个是官方的文档
public class ExampleAppWidgetProvider extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); views.setOnClickPendingIntent(R.id.button, pendingIntent); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } }}//如果是要刷新Appwidget界面的组件或者信息,看以下代码
public class MyAppWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if("org.first.demo".equals(intent.getAction){
RemoteView remote=new RemoteView(context.PackageName(),R.layout.app_provider)
remote.setImageViewViewResource(R.id.img,R.drawable.mldn_man)
remote.setTextView(R.id.textView,"要替换成这些文字");
AppwidgetManager appwidgetManager=AppWidgetManager.getInstance(context);
ComponentName conponenName=new ComponenName(context,MainActivity.class);
appwidgetManager.updateAppWidget(conponenName,remote)
}else{
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent it=new Intent();
it.setAction("org.first.demo");
//注意这个PendingIntent与上面的区别上面的是getActivity指定跳转的Activity,这个是getBroadCast是更新Appwidget
PendingIntent pending=PendingIntent.getBroadcast(context,0,it,PendingIntent.FLAG_UPDATE_CURRENT);
RemoteView remote=new RemoteView(context.PackName(),R.layout.app_widget);
remote.setOnCllickPendingIntent(R.id.app_area,pending)
appWidgetManager.updateAppWidget(appWidgetId, remote);
}
}
}
//同时在androidManifest.xml中修改文件
<receiver android:name=".MyAppWidget">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<intent-filter >
<action android:name="org.first.demo"
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/my_appwidget"/>
</receiver>
- android桌面组件AppWidget
- 桌面组件AppWidget
- Android--桌面组件AppWidget
- AppWidget(桌面组件)
- 理解与应用Android桌面组件AppWidget
- appwidget桌面小部件
- appwidget桌面小部件
- android 桌面控件appwidget
- 桌面组件Appwidget的应用实例以及导入第三方jar包的问题解决
- Android AppWidget组件
- 如何在桌面添加AppWidget
- Android:AppWidget 桌面小控件
- 【Android】AppWidget桌面小部件
- 代码方式桌面添加AppWidget
- Android桌面小部件AppWidget
- Android 自定义桌面空间 AppWidget
- appWidget(桌面小部件)
- AppWidget组件的处理事件
- CORBA异常
- 飞鸽传书就不去袭击你们了
- HTML中ul,ol,li,dl,dt,dd标签用法
- JS弹出层来对表单数据进行编辑
- HEVC/H265 namespace 介绍
- 桌面组件AppWidget
- 移植OpenSSH 到嵌入式操作系统当中
- Firebug中请求各阶段时间详细解释
- 小Baby学说话的几个阶段
- Objective-C语法之KVC的使用
- KindEdito使用细节
- 转载:x86的cpu_relax解析
- ORACLE中的rownum排序及row_number()排序的区别
- Java类中各成员的加载和内存存放位置