因为AppWidgetProvider扩展自BroadcastReceiver, 所以你不能保证回调函数完成调用后,AppWidgetProvider还在继续运行。
a. AppWidgetProvider 的实现
/**
* Copyright(C):教育电子有限公司
* Project Name: NineSync
* Filename: SynWidgetProvider.java
* Author(S): Rjdeng
* Created Date: 2013-4-23 下午8:55:42
* Version: V1.00
* Description: 九科同步挂件
*/
package com.eebbk.synstudy.widget;
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;
import com.eebbk.synstudy.R;
public class SynWidgetProvider extends AppWidgetProvider
{
public static final String SUBJECT_MARK = "subject";
private static final int CHINESE_CONSTANT = -1;
private final String NINESYNC_PKG = "com.eebbk.synstudy";
private final String NINESYNC_CLS = "com.eebbk.synstudy.welcome.WelcomActivity";
private final String STUDYCARS_PKG = "com.eebbk.readingcard.readingcard";
private final String STUDYCARS_CLS = "com.eebbk.readingcard.readingcard.MenuManagerActivity";
private final int widgetViewId[] = { R.id.syn_widget_notes, R.id.syn_widget_chinese, R.id.syn_widget_math,
R.id.syn_widget_english, R.id.syn_widget_physics, R.id.syn_widget_chemistry, R.id.syn_widget_organisms,
R.id.syn_widget_history, R.id.syn_widget_geography, R.id.syn_widget_political };
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
// TODO Auto-generated method stub
System.out.printf( "###九科同步挂件创建\n" );
final RemoteViews views = new RemoteViews( context.getPackageName( ), R.layout.main_syn_widget );
setViewOnClick( context, views );
pushUpdate( context, appWidgetIds, views );
super.onUpdate( context, appWidgetManager, appWidgetIds );
}
@Override
public void onDeleted( Context context, int[] appWidgetIds )
{
// TODO Auto-generated method stub
System.out.printf( "###九科同步挂件删除\n" );
super.onDeleted( context, appWidgetIds );
}
@Override
public void onEnabled( Context context )
{
// TODO Auto-generated method stub
System.out.printf( "###当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用\n" );
super.onEnabled( context );
}
private void setViewOnClick( Context context, RemoteViews views )
{
Intent intent;
PendingIntent pendingIntent;
ComponentName componentName;
//九科同步
componentName = new ComponentName( NINESYNC_PKG, NINESYNC_CLS );
for ( int i = 1; i < widgetViewId.length; i++ )
{
intent = new Intent( );
intent.setComponent( componentName );
intent.putExtra( SUBJECT_MARK, CHINESE_CONSTANT + i );
//for putExtra
intent.setAction( String.valueOf( System.currentTimeMillis( ) ) );
pendingIntent = PendingIntent.getActivity( context, 0 /* no requestCode */, intent, 0 /* no flags */);
views.setOnClickPendingIntent( widgetViewId[i], pendingIntent );
}
//读书卡片
intent = new Intent( );
componentName = new ComponentName( STUDYCARS_PKG, STUDYCARS_CLS );
intent.setComponent( componentName );
pendingIntent = PendingIntent.getActivity( context, 0 /* no requestCode */, intent, 0 /* no flags */);
views.setOnClickPendingIntent( widgetViewId[0], pendingIntent );
}
private void pushUpdate( Context context, int[] appWidgetIds, RemoteViews views )
{
// Update specific list of appWidgetIds if given, otherwise default to all
final AppWidgetManager gm = AppWidgetManager.getInstance( context );
if( appWidgetIds != null )
{
gm.updateAppWidget( appWidgetIds, views );
}
else
{
gm.updateAppWidget( new ComponentName( context, this.getClass( ) ), views );
}
}
}
b. widget外观布局定义文件(文件位置:res\layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/syn_widget_bg"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dip" >
<Button
android:id="@+id/syn_widget_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/readnotes_selector" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dip"
android:layout_marginTop="100dip"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/syn_widget_chinese"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/chinese_selector"
android:clickable="true" />
<ImageButton
android:id="@+id/syn_widget_math"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/math_selector" />
<ImageButton
android:id="@+id/syn_widget_english"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/english_selector" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/syn_widget_physics"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/physics_selector" />
<ImageButton
android:id="@+id/syn_widget_chemistry"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/chemistry_selector" />
<ImageButton
android:id="@+id/syn_widget_organisms"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/organisms_selector" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/syn_widget_history"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/history_selector" />
<ImageButton
android:id="@+id/syn_widget_geography"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/geography_selector" />
<ImageButton
android:id="@+id/syn_widget_political"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/political_selector" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
c. 新增widget时的配置Activity的实现(可选)本介绍没有选择这种方式
d. widget 参数配置文件(文件位置:res\xml)
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/main_syn_widget"
android:minHeight="505dip"
android:minWidth="729dip"
android:previewImage="@drawable/sync_launcher"
android:updatePeriodMillis="0" >
</appwidget-provider>
e. AndroidManifest.xml 声明
<receiver
android:name=".widget.SynWidgetProvider"
android:permission="android.permission.BIND_APPWIDGET" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/syn_widget_config" />
</receiver>
f. 效果图(九科同步挂件Rjdeng 于2013-4-24)
觉得本文对你有用,麻烦点赞或关注或收藏,你的肯定是我创作的无限动力,谢谢!!!