首先我们需要创建一个NewAppWidget类,需要继承父类AppWidgetProvider,AppWidgetProvider一共有4个方法:
-
public void onUpdate(Context context....
-
public void onEnabled(Context context) {...
-
public void onDisabled(Context context) {...
-
public void onDisabled(Context context) {...
我觉得看方法的名字大致上都猜出了是什么意思吧?
然后在androidmanifest的说明文件中申明如下:
<receiver android:name=".NewAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
请注意上面代码种红色的字体,这个是配置文件,主要的代码如下:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="250dp"//宽度 android:minHeight="180dp"//高度 android:updatePeriodMillis="86400000"//一天更新一次 android:previewImage="@drawable/example_appwidget_preview"//这是一张图片 android:initialLayout="@layout/new_app_widget"//布局文件 android:resizeMode="horizontal|vertical" android:widgetCategory="home_screen" android:configure="com.lanxin.testwidget.MainActivity"//这里还申明了一个配置文件,在实际应用中,我们可能用不到配置文件,因为都是后台联网读取数据的,但是这里为了演示,使用配置文件比较适合. android:initialKeyguardLayout="@layout/new_app_widget"></appwidget-provider>
现在我们来编写NewAppWidget类的代码:
package com.lanxin.testwidget; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.pm.PackageManager; import android.util.Log; import android.widget.RemoteViews; /** * Implementation of App Widget functionality. */ public class NewAppWidget extends AppWidgetProvider { private static final String TAG = "NewAppWidget"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { updateAppWidget(context, appWidgetManager, appWidgetIds[i]); } Log.d(TAG,"onUpdate"); } @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(new ComponentName("com.lanxin.testwidget",".NewAppWidget") ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP); Log.d(TAG, "onEnabled"); } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(new ComponentName("com.lanxin.testwidget",".NewAppWidget"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP); Log.d(TAG, "onDisabled"); } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { ConfigEx configEx = new ConfigEx(context,"widget"); CharSequence widgetText = configEx.getKeyStr("txt"); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); Log.d(TAG, "updateAppWidget"); } }
接着贴上配置器的代码:
package com.lanxin.testwidget; import android.app.Activity; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RemoteViews; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "WidgetLog"; EditText edit; Button save; ConfigEx mConfig; Context mContext; int mAppwidgetID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bundle bundle = getIntent().getExtras(); if(bundle!=null){ mAppwidgetID = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); Log.d(TAG,"mAppwidgetID:"+mAppwidgetID); } init(); } private void init() { mContext = this; mConfig = new ConfigEx(mContext,"widget"); edit = (EditText)findViewById(R.id.edit); save = (Button)findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mConfig.putKey("txt", edit.getText().toString()); updateWidget(AppWidgetManager.getInstance(mContext), edit.getText().toString()); //更新完毕后,返回ID给appwidget,不然无法更新 Intent intent = new Intent(); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppwidgetID); setResult(RESULT_OK,intent); toast("保存成功!"); finish(); } }); } private void toast(String msg){ Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show(); } public void updateWidget(AppWidgetManager appWidgetManager,String str){ //创建一个RemoteViews对象 RemoteViews rv = new RemoteViews(mContext.getPackageName(),R.layout.new_app_widget); //操作对象中的内容 rv.setTextViewText(R.id.appwidget_text,str); //创建一个pendingIntent对象,用来绑定时间 //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://lanxinbase.com")); Intent intent = new Intent(this,MainActivity.class);//当然,也可以打开窗口 PendingIntent pi = PendingIntent.getActivity(mContext,0,intent,0); //绑定事件 rv.setOnClickPendingIntent(R.id.but,pi); //执行更新 appWidgetManager.updateAppWidget(mAppwidgetID,rv); } }
下面是效果图:
源码:http://pan.baidu.com/s/1sl7XPiX