android:桌面widget,桌面记事本
实现功能:桌面创建多个记事,每个记事本可以分别更新内容
1,manifest.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.edl.mininote" android:versionCode="1" android:versionName="1.0"> 4 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> 5 <!-- 设置发送短信的权限 --> 6 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 7 <uses-permission android:name="android.permission.MOUT_UNMOUNT_FILESYSTEMS"/> 8 9 <application 10 android:icon="@drawable/icon" android:label="@string/app_name" > 11 12 <receiver android:name=".MiniWidgetProvider"> 13 <meta-data android:name="android.appwidget.provider" 14 android:resource="@xml/widget_provider" /> 15 <intent-filter> 16 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 17 </intent-filter> 18 </receiver> 19 20 <activity android:name=".MiniNoteConfige" 21 android:theme="@android:style/Theme.Black.NoTitleBar" 22 android:configChanges="keyboardHidden|orientation|screenLayout" 23 > 24 <intent-filter> 25 <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/> 26 </intent-filter> 27 </activity> 28 </application> 29 </manifest>
2,xml配置文件:(res中新建xml文件夹,新建文件内容如下)
1 <?xml version="1.0" encoding="utf-8"?> 2 <appwidget-provider 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:minWidth="120dip" 5 android:minHeight="150dip" 6 android:updatePeriodMillis="0" 7 android:configure="com.edl.mininote.MiniNoteConfige" //打开文件首先进入的activity 8 android:initialLayout="@layout/mininote" //布局文件 9 />
minWidth: 定义Wdiget组件的宽度
minHeight: 定义Wdiget组件的高度
updatePeriodMillis: 更新的时间周期,如果不需要定时更新值设置为0
android:configure="com.edl.mininote.MiniNoteConfige" 如果需要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名
android:initialLayout="@layout/mininote" 此句为指定桌面组件的布局文件。
3,layout:文件:
<1> mininote.xml:

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:background="@drawable/edit_bg_yellow" 5 android:focusable="true" 6 android:clickable="true" 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:id="@+id/mininote_rl"> 10 <ImageView 11 android:id="@+id/new_btn" 12 android:layout_width="60dip" 13 android:layout_height="25dip" 14 android:layout_marginTop="8dip" 15 android:layout_marginRight="-5dip" 16 android:src="@drawable/postit_s1_add_yellow" 17 android:scaleType="matrix" 18 android:paddingLeft="35dip" 19 android:layout_alignParentTop="true" 20 android:layout_alignParentRight="true" /> 21 <TextView 22 android:focusable="true" android:clickable="true" 23 android:textColor="#ff000000" 24 android:id="@+id/body_txt" 25 android:fadingEdge="vertical" 26 android:fadingEdgeLength="25.0dip" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_marginLeft="5.0dip" 30 android:layout_marginTop="24.0dip" 31 android:layout_marginRight="3.0dip" 32 android:layout_marginBottom="5.0dip" 33 android:layout_alignParentLeft="true" 34 android:layout_alignParentTop="true" 35 android:lineSpacingExtra="5.0px" /> 36 37 <ImageView 38 android:visibility="gone" 39 android:layout_gravity="center" 40 android:id="@+id/no_data_img" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:layout_marginLeft="2.0px" 44 android:layout_marginTop="5.0dip" 45 android:layout_toRightOf="@id/body_txt" 46 android:layout_alignTop="@id/body_txt" 47 android:src="@drawable/memo_noiconimage_18" /> 48 </RelativeLayout>
<2>editer.xml:

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/launch_linear" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 android:background="@drawable/edit_bg_yellow" 8 android:orientation="vertical" 9 > 10 <LinearLayout 11 android:layout_height="fill_parent" 12 android:layout_width="fill_parent" 13 android:layout_weight="2"> 14 <view 15 class="com.edl.mininote.EditView" 16 android:id="@+id/et_content" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="30dip" 20 style="?android:attr/textViewStyle" 21 android:background="@android:color/transparent" 22 android:padding="5dip" 23 android:scrollbars="vertical" 24 android:fadingEdge="vertical" 25 android:gravity="top|left" 26 android:editable="true" 27 android:cursorVisible="true" 28 android:textColor="@color/black" 29 android:textSize="22sp" 30 android:capitalize="sentences" 31 android:imeOptions="actionDone" 32 /> 33 </LinearLayout> 34 35 <LinearLayout 36 android:layout_width="fill_parent" 37 android:layout_height="60dip" 38 android:orientation="horizontal" 39 android:layout_marginTop="2dip" 40 android:layout_marginBottom="2dip" 41 android:layout_weight="1" 42 > 43 <View 44 android:layout_width="3dip" 45 android:layout_height="fill_parent" /> 46 <Button 47 android:id="@+id/save" 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:background="@drawable/selector" 51 android:focusable="true" 52 android:text="@string/save" 53 android:textColor="@color/white" 54 android:textSize="16.0dip" 55 android:layout_weight="1" /> 56 <Button 57 android:id="@+id/cancel" 58 android:layout_width="fill_parent" 59 android:layout_height="wrap_content" 60 android:background="@drawable/selector" 61 android:focusable="true" 62 android:nextFocusRight="@id/cancel" 63 android:text="@string/cancel" 64 android:textColor="@color/white" 65 android:textSize="16.0dip" 66 android:layout_weight="1" /> 67 <View 68 android:layout_width="3dip" 69 android:layout_height="fill_parent" /> 70 </LinearLayout> 71 </LinearLayout>
4,java文件:
<1> 继承自AppWidgetProvider的类

1 public class MiniWidgetProvider extends AppWidgetProvider { 2 private static String TAG = "MiniWidgetProvider"; 3 final String mPerfName = "com.edl.mininote.MiniNote"; 4 private RemoteViews views ; 5 private static String CLICK_NAME_ACTION = "com.edl.mininote.action.APPWIDGET_CLICKED"; 6 7 // private int memoId,color; 8 @Override 9 public void onUpdate(Context context, AppWidgetManager appWidgetManager, 10 int[] appWidgetIds) { 11 DBHelper helper = new DBHelper(context); 12 for (int appWidgetId : appWidgetIds) { 13 int[] specificAppWidget = new int[] { appWidgetId }; 14 Log.i(TAG, "onUpdate------specificAppWidget= " + specificAppWidget); 15 RemoteViews views = updateWidget(context, appWidgetId, helper); 16 appWidgetManager.updateAppWidget(specificAppWidget, views); 17 } 18 helper.close(); 19 } 20 21 @Override 22 public void onReceive(Context context, Intent intent) { 23 Log.i(TAG, "onReceive----------"); 24 super.onReceive(context, intent); 25 } 26 27 @Override 28 public void onDeleted(Context context, int[] appWidgetIds) { 29 // TODO Auto-generated method stub 30 DBHelper helper = new DBHelper(context); 31 for (int appWidgetId : appWidgetIds) { 32 int[] specificAppWidget = new int[] { appWidgetId }; 33 helper.deleteNote(appWidgetId); 34 } 35 helper.close(); 36 } 37 38 public static RemoteViews updateWidget(Context context, int appWidgetId, DBHelper helper){ 39 Log.i(TAG, "buildUpdate------appWidgetId= " + appWidgetId); 40 boolean isEmpty = false; 41 String text = helper.getText(appWidgetId); 42 int color = helper.getColor(appWidgetId); 43 if (!TextUtils.isEmpty(text)) {//text != null 44 Log.i(TAG, "buildUpdate------text != null "); 45 RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mininote); 46 views.setTextViewText(R.id.body_txt, text); 47 views.setViewVisibility(R.id.no_data_img, View.GONE); 48 Intent sendintent = new Intent(context, MiniNoteConfige.class); 49 Log.i("sendintent", "sendintent"); 50 sendintent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 51 sendintent.putExtra("hasindb", true); 52 sendintent.putExtra("text", text); 53 sendintent.setAction(String.valueOf(appWidgetId)); 54 //sendintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); 55 PendingIntent temp = PendingIntent.getActivity(context, 0, 56 sendintent,0); 57 views.setOnClickPendingIntent(R.id.body_txt,temp); 58 views.setOnClickPendingIntent(R.id.mininote_rl,temp); 59 isEmpty = false; 60 views = setColor(views,color,isEmpty); 61 // views.setImageViewBitmap(R.id.TextView01, text); 62 return views; 63 } else { 64 RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mininote); 65 String defaulttext = context.getString(R.string.app_name); 66 views.setTextViewText(R.id.body_txt, defaulttext); 67 views.setViewVisibility(R.id.no_data_img, View.VISIBLE); 68 Intent sendintent = new Intent(context, MiniNoteConfige.class); 69 Log.i("sendintent", "sendintent"); 70 sendintent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 71 sendintent.putExtra("hasindb", true); 72 sendintent.putExtra("text", text); 73 sendintent.setAction(String.valueOf(appWidgetId)); 74 //sendintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); 75 PendingIntent temp = PendingIntent.getActivity(context, 0, 76 sendintent,0); 77 views.setOnClickPendingIntent(R.id.body_txt,temp); 78 views.setOnClickPendingIntent(R.id.mininote_rl,temp); 79 isEmpty = true; 80 views = setColor(views,color,isEmpty); 81 return views; 82 } 83 } 84 //设置背景显示 85 …………………… 86 87 }
<2> edit界面:MiniNoteConfige.java //打开widget进入的activity

1 public class MiniNoteConfige extends Activity implements OnClickListener { 2 private String TAG = "MiniNoteConfige"; 3 4 // final String mPerfName = "com.edl.mininote.MiniNoteConf"; 5 // public static final Uri CONTENT_URI = Uri.parse("content://com.edl.mininote.provider/memo"); 6 7 private int mAppWidgetId = -1; 8 private String content; 9 boolean hasindb; 10 boolean isSaveInDb = true; 11 12 private Button mSaveBut; 13 private Button mCancelBut; 14 private EditText mEditText; 15 private LinearLayout mLLContent; 16 17 public int oldColor= 0 ; 18 public int newColor = -1; 19 20 private ArrayList<Map<String,Object>> colorItem ; 21 private static final int Menu_send = Menu.FIRST; 22 private static final int Menu_save = Menu.FIRST + 1; 23 private static final int Menu_color = Menu.FIRST + 2; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 setContentView(R.layout.editer); 28 mSaveBut = (Button) findViewById(R.id.save); 29 mSaveBut.setOnClickListener(this); 30 mCancelBut = (Button) findViewById(R.id.cancel); 31 mCancelBut.setOnClickListener(this); 32 mEditText = (EditText) findViewById(R.id.et_content); 33 mLLContent =( LinearLayout) findViewById(R.id.launch_linear); 34 35 mAppWidgetId = getIntent().getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 36 hasindb = getIntent().getBooleanExtra("hasindb", false); 37 // text = getIntent().getStringExtra("text"); 38 39 DBHelper helper = new DBHelper(this); 40 isSaveInDb = helper.checkIfSave(mAppWidgetId); 41 if(!isSaveInDb){//没有保存到数据库 42 Log.i(TAG, "not in db"); 43 } 44 45 content = helper.getText(mAppWidgetId); 46 oldColor = helper.getColor(mAppWidgetId); 47 setBgColor(mLLContent, oldColor); 48 helper.close(); 49 50 Log.i(TAG, "mAppWidgetId = " + mAppWidgetId + ",hasindb = " + hasindb + ",text = " + content); 51 mEditText.setText(content); 52 // 定位光标位置 53 int selection_end = (mEditText.getText().toString()).length(); 54 mEditText.setSelection(selection_end); 55 56 if (mAppWidgetId == -1) { 57 setResult(Activity.RESULT_CANCELED); 58 finish(); 59 } 60 61 if(hasindb == false){ 62 DBHelper helper1 = new DBHelper(this); 63 if (helper1.insertNote(mAppWidgetId, (mEditText.length() > 0?mEditText.getText().toString():""),0,hasindb)) { 64 // Push newly updated widget to surface 65 RemoteViews views = MiniWidgetProvider.updateWidget(this, 66 mAppWidgetId, helper1); 67 AppWidgetManager appWidgetManager = AppWidgetManager 68 .getInstance(this); 69 appWidgetManager.updateAppWidget(new int[] { mAppWidgetId }, views); 70 } 71 helper1.close(); 72 73 // Make sure we pass back the original mAppWidgetId 74 Intent resultValue = new Intent(); 75 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 76 setResult(RESULT_OK, resultValue); 77 finish(); 78 } 79 super.onCreate(savedInstanceState); 80 } 81 82 @Override 83 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 84 super.onActivityResult(requestCode, resultCode, data); 85 } 86 87 @Override 88 public void onClick(View v) { 89 switch ((Integer) v.getId()) { 90 case R.id.save: 91 Log.i(TAG, "onClick to save------------mAppWidgetId= " + mAppWidgetId + ",text = " + mEditText.getText().toString() ); 92 int color; 93 if(newColor != -1){ 94 color = newColor; 95 }else{ 96 color = oldColor; 97 } 98 DBHelper helper = new DBHelper(this); 99 if (helper.insertNote(mAppWidgetId, (mEditText.length() > 0?mEditText.getText().toString():""),color,hasindb)) { 100 Log.i(TAG, "save success====="); 101 102 // Push newly updated widget to surface 103 RemoteViews views = MiniWidgetProvider.updateWidget(this, 104 mAppWidgetId, helper); 105 AppWidgetManager appWidgetManager = AppWidgetManager 106 .getInstance(this); 107 appWidgetManager.updateAppWidget(new int[] { mAppWidgetId }, views); 108 } 109 helper.close(); 110 111 // Make sure we pass back the original mAppWidgetId 112 Intent resultValue = new Intent(); 113 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 114 setResult(RESULT_OK, resultValue); 115 finish(); 116 117 break; 118 case R.id.cancel : 119 Intent resultValue1 = new Intent(); 120 resultValue1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 121 setResult(RESULT_OK, resultValue1); 122 finish(); 123 break; 124 default: 125 break; 126 } 127 } 128 ………………………… 129 }
这个东西不难,但是配置比较麻烦,新手的我竟然做了两周,最后还有一个问题没弄出来,桌面上点击一个按钮新建一个widget这个还不知道怎么做,有谁知道的话谢谢赐教!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端