小子,当你看到本文章时尼玛就是幸运儿啦!
桌面便签软件听过吗?见过吗?
没错,就是android上常用的软件之一,比如曾经的Sticky Note,就曾非常流行,Sticky Note的介绍可以参见 http://www.tompda.com/c/article/11778/
而实际上使用android平台对widget开发的支持,桌面便签类软件是非常易于开发的。
本文通过逐步实现一个简单的桌面便签软件,和大家分享进行widget开发的过程和方法。
1.MyNote的最终实现效果
Come on !为了勾引大家阅读本文的兴趣,先白活一下最终效果。首先可以通过桌面增加我们的MyNote小部件,如下图所示:
图中的“我的便签”就是我们之后将要开发的便签程序。 点击后启动添加日志界面,如下图所示:
输入便签内容后,可以点击下面所列的四种图标之一作为便签图标。比如点击第一个后,桌面上就会添加一个便签:
点击桌面上的便签,可以再次对便签内容进行修改,并更换图标。桌面上可以同时存在多个便签,并可以分别进行修改。
如下图所示,我们将刚才创建的便签的图标修改一下,并新增了一个便签:
每个便签的内容都是分别独立保存的,可以随时点击桌面图标修改。
2.开发方式
开发的目的和追求的效果已经十分明,确定一下开发方式。
本人将采取一种渐进式的开发,尼玛听过吗?也就是说不会一口气从头做到尾。
而是分为好几个阶段。每个阶段都完成一定的目标,然后下个阶段增加更多的功能,
每个阶段都离最终目标更进一步,OK,你可以说这是一次敏捷开发 !
第一个阶段,首先我们会搭建一个widget原型程序,它是完全可以运行的,可以创建桌面widget。
第二个阶段,我们改进 widget 配置Activity 部分的实现使其具备创建便签的功能
第三个阶段,我们改进 widget 点击响应部分的实现,使其具备修改便签的功能
3.搭建widget原型程序
本节我们会做一个最简单的widget程序原型,但是它是可以运行的。
一般来说 widget 程序由以下部分组成:
a. AppWidgetProvider 的实现
b. widget外观布局定义文件
c. 新增widget时的配置Activity的实现(可选)
d. widget 参数配置文件
以下分别讲解
a. AppWidgetProvider 的实现
Let's go!新建一个android工程MyNote,然后修改 MyNote.java 的代码,
使MyNote继承自 AppWidgetProvider ,并重写 onUpdate 和 onDeleted 方法。
其中onUpdate 会在widget创建及被更新时调用, onDeleted 会在widget被删除时调用。
目前我们不需要在这里实现任何功能,只是简单的记录日志以便我们观察其运行,编写好的代码如下:
package sun.geoffery.mynote; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.os.Bundle; import android.util.Log; public class MyNote extends AppWidgetProvider { /* 在widget创建及被更新时调用 */ @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]; Log.i("myLog", "this is [" + appWidgetId + "] onUpdate!"); } } /* 在widget被删除时调用 */ @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; Log.i("myLog", "this is [" + appWidgetId + "] onDeleted!"); } } }
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_widget_img" android:layout_width="wrap_content" android:layout_height="match_parent" android:clickable="true" android:src="@drawable/empty_sketchy" />
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_widget_img" android:layout_width="wrap_content" android:layout_height="match_parent" android:clickable="true" android:src="@drawable/empty_sketchy" />
package sun.geoffery.mynote; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Intent; import android.os.Bundle; import android.util.Log; /** * 一般来说,这个配置界面的作用是用户新建widget时,让用户配置widget的一些属性,比如颜色、大小等等。 * @author GeofferySun */ public class MyNoteConf extends Activity { int mAppwidgetId; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Log.i("myLog", "on WidgetCof..."); setResult(RESULT_CANCELED); // Find the widget id from the intent. Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mAppwidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, // AppWidgetManager.INVALID_APPWIDGET_ID); } // If they gave us an intent without the widget id, just bail. if (mAppwidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } // return OK Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppwidgetId); setResult(RESULT_OK, resultValue); finish(); } }
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:configure="sun.geoffery.mynote.MyNoteConf" android:initialLayout="@layout/my_note_widget" android:minHeight="72dp" android:minWidth="72dp" android:updatePeriodMillis="86400000" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sun.geoffery.mynote" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/empty_sketchy" android:label="@string/app_name" > <receiver android:name=".MyNote" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_note_widget" /> </receiver> <activity android:name=".MyNoteConf" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/note_title" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left" android:hint="@string/editehint" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:gravity="center" > <ImageButton android:id="@+id/imagebutton1" android:layout_width="72dp" android:layout_height="72dp" android:layout_margin="3dp" android:src="@drawable/comment_bubble" /> <ImageButton android:id="@+id/imagebutton2" android:layout_width="72dp" android:layout_height="72dp" android:layout_margin="3dp" android:src="@drawable/computer_monitor" /> <ImageButton android:id="@+id/imagebutton3" android:layout_width="72dp" android:layout_height="72dp" android:layout_margin="3dp" android:src="@drawable/email" /> <ImageButton android:id="@+id/imagebutton4" android:layout_width="72dp" android:layout_height="72dp" android:layout_margin="3dp" android:src="@drawable/home_page" /> </LinearLayout> </LinearLayout>
package sun.geoffery.mynote; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.RemoteViews; /** * 一般来说,这个配置界面的作用是用户新建widget时,让用户配置widget的一些属性,比如颜色、大小等等。 * @author GeofferySun */ public class MyNoteConf extends Activity { int mAppwidgetId; private ImageButton imagebutton1, imagebutton2, imagebutton3, imagebutton4; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_note_conf); Log.i("myLog", "on WidgetCof..."); setResult(RESULT_CANCELED); // Find the widget id from the intent. Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mAppwidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, // AppWidgetManager.INVALID_APPWIDGET_ID); } // If they gave us an intent without the widget id, just bail. if (mAppwidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } imagebutton1 = (ImageButton) findViewById(R.id.imagebutton1); imagebutton2 = (ImageButton) findViewById(R.id.imagebutton2); imagebutton3 = (ImageButton) findViewById(R.id.imagebutton3); imagebutton4 = (ImageButton) findViewById(R.id.imagebutton4); imagebutton1.setOnClickListener(new imagebuttonOnClickListener()); imagebutton2.setOnClickListener(new imagebuttonOnClickListener()); imagebutton3.setOnClickListener(new imagebuttonOnClickListener()); imagebutton4.setOnClickListener(new imagebuttonOnClickListener()); } private final class imagebuttonOnClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub int srcId = R.drawable.empty_sketchy; switch (v.getId()) { case R.id.imagebutton1: srcId = R.drawable.comment_bubble; break; case R.id.imagebutton2: srcId = R.drawable.computer_monitor; break; case R.id.imagebutton3: srcId = R.drawable.email; break; case R.id.imagebutton4: srcId = R.drawable.home_page; break; } Log.i("myLog", "mAppWidgetId is:" + mAppwidgetId); RemoteViews views = new RemoteViews( MyNoteConf.this.getPackageName(),// R.layout.my_note_widget); views.setImageViewResource(R.id.my_widget_img, srcId); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(MyNoteConf.this); appWidgetManager.updateAppWidget(mAppwidgetId, views); // return OK Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppwidgetId); setResult(RESULT_OK, resultValue); finish(); } } }
package sun.geoffery.mynote; import android.app.Activity; import android.os.Bundle; public class MyNoteEdit extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_note_conf); } }
package sun.geoffery.mynote; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.RemoteViews; public class MyNoteEdit extends Activity { int mAppwidgetId; private ImageButton imagebutton1, imagebutton2, imagebutton3, imagebutton4; private EditText edittext; private String mPerfName = "sun.geoffery.mynote.MyNoteConf"; private SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_note_conf); Intent intent = getIntent(); Log.i("MyLog", intent.getAction()); mAppwidgetId = intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, -1); Log.i("MyLog", "it's[" + mAppwidgetId + "] ending!"); mPref = getSharedPreferences(mPerfName, 0); String noteContent = mPref.getString("DAT" + mAppwidgetId, ""); edittext = (EditText) findViewById(R.id.edittext); edittext.setText(noteContent); imagebutton1 = (ImageButton) findViewById(R.id.imagebutton1); imagebutton2 = (ImageButton) findViewById(R.id.imagebutton2); imagebutton3 = (ImageButton) findViewById(R.id.imagebutton3); imagebutton4 = (ImageButton) findViewById(R.id.imagebutton4); imagebutton1.setOnClickListener(new imagebuttonOnClickListener()); imagebutton2.setOnClickListener(new imagebuttonOnClickListener()); imagebutton3.setOnClickListener(new imagebuttonOnClickListener()); imagebutton4.setOnClickListener(new imagebuttonOnClickListener()); } private final class imagebuttonOnClickListener implements OnClickListener { @Override public void onClick(View v) { SharedPreferences.Editor prefsEdit = mPref.edit(); prefsEdit.putString("DAT" + mAppwidgetId, edittext.getText() .toString()); prefsEdit.commit(); // TODO Auto-generated method stub int srcId = R.drawable.empty_sketchy; switch (v.getId()) { case R.id.imagebutton1: srcId = R.drawable.comment_bubble; break; case R.id.imagebutton2: srcId = R.drawable.computer_monitor; break; case R.id.imagebutton3: srcId = R.drawable.email; break; case R.id.imagebutton4: srcId = R.drawable.home_page; break; } RemoteViews views = new RemoteViews( MyNoteEdit.this.getPackageName(),// R.layout.my_note_widget); views.setImageViewResource(R.id.my_widget_img, srcId); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(MyNoteEdit.this); appWidgetManager.updateAppWidget(mAppwidgetId, views); MyNoteEdit.this.finish(); } } }