完成摘要事件监控小部件

This commit is contained in:
ZhanGSKen 2025-02-15 19:53:24 +08:00
parent bbac0c7306
commit 7a8d3329d4
10 changed files with 221 additions and 13 deletions

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Sat Feb 15 04:45:53 GMT 2025
#Sat Feb 15 11:51:34 GMT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.1
buildCount=97
buildCount=123
baseBetaVersion=1.5.2

View File

@ -60,7 +60,26 @@
</intent-filter>
</receiver>
<receiver android:name=".widgets.TimeWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.android.UPDATE_TIME" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info" />
</receiver>
<receiver android:name=".TimeWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.android.UPDATE_TIME" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info" />
</receiver>
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>

View File

@ -4,10 +4,13 @@ package cc.winboll.studio.appbase.threads;
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/14 03:46:44
*/
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import cc.winboll.studio.appbase.handlers.MainServiceHandler;
import cc.winboll.studio.appbase.widgets.TimeWidget;
import cc.winboll.studio.libappbase.LogUtils;
import com.hjq.toast.ToastUtils;
import java.lang.ref.WeakReference;
public class MainServiceThread extends Thread {
@ -38,11 +41,20 @@ public class MainServiceThread extends Thread {
@Override
public void run() {
LogUtils.d(TAG, "run()");
while (!isExist()) {
//ToastUtils.show("run()");
//LogUtils.d(TAG, "run()");
Intent intent = new Intent(mContext, TimeWidget.class);
intent.setAction(TimeWidget.UPDATE_TIME_ACTION);
//PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mContext.sendBroadcast(intent);
//AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
//long interval = 1000;
//alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, pendingIntent);
try {
Thread.sleep(3000);
Thread.sleep(1000);
} catch (InterruptedException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}

View File

@ -0,0 +1,85 @@
package cc.winboll.studio.appbase.widgets;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/15 14:41:25
* @Describe TimeWidget
*/
import android.app.AlarmManager;
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 cc.winboll.studio.appbase.R;
import cc.winboll.studio.libappbase.LogUtils;
import com.hjq.toast.ToastUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
public class TimeWidget extends AppWidgetProvider {
public static final String TAG = "TimeWidget";
public static final String UPDATE_TIME_ACTION = "com.example.android.UPDATE_TIME";
volatile static ArrayList<String> _Message;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(UPDATE_TIME_ACTION)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TimeWidget.class));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
}
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
if (_Message == null) {
_Message = new ArrayList<String>();
}
LogUtils.d(TAG, "updateAppWidget(...)");
StringBuilder sbLine = new StringBuilder();
// for (int appWidgetId : appWidgetIds) {
// RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//
// appWidgetManager.updateAppWidget(appWidgetId, views);
// }
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//设置按钮点击事件
Intent intent = new Intent(context, WidgetButtonClickListener.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
// 获取当前时间并设置到TextView
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
sbLine.append("[");
sbLine.append(currentTime);
sbLine.append("]");
_Message.add(0, sbLine.toString());
while(_Message.size() > 6) { // 控制显示在6行
_Message.remove(_Message.size() - 1);
}
String message = String.join("\n", _Message);
views.setTextViewText(R.id.timeTextView, message);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

View File

@ -0,0 +1,34 @@
package cc.winboll.studio.appbase.widgets;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/15 17:20:46
* @Describe WidgetButtonClickListener
*/
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
import cc.winboll.studio.appbase.R;
public class WidgetButtonClickListener extends BroadcastReceiver {
public static final String TAG = "WidgetButtonClickListener";
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TimeWidget.class));
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.timeTextView, "文本已更新");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Toast.makeText(context, "按钮被点击", Toast.LENGTH_SHORT).show();
}
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFFFF">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/widget_button"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Text"
android:id="@+id/timeTextView"
android:layout_weight="1.0"/>
</LinearLayout>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="200dp"
android:minHeight="100dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/widget_layout">
</appwidget-provider>

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Sat Feb 15 04:45:53 GMT 2025
#Sat Feb 15 11:51:34 GMT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.1
buildCount=97
buildCount=123
baseBetaVersion=1.5.2

View File

@ -7,7 +7,6 @@ package cc.winboll.studio.libappbase;
*/
import android.content.Context;
import android.content.Intent;
import com.hjq.toast.ToastUtils;
public class SOS {

View File

@ -1,6 +1,7 @@
package cc.winboll.studio.libappbase.receiver;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import cc.winboll.studio.libappbase.LogUtils;
@ -12,19 +13,38 @@ import cc.winboll.studio.libappbase.WinBoll;
* @Describe WinBollReceiver
*/
public class WinBollReceiver extends BroadcastReceiver {
public static final String TAG = "WinBollReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(WinBoll.ACTION_SOS)) {
if (action.equals(WinBoll.ACTION_SOS)) {
LogUtils.d(TAG, String.format("context.getPackageName() %s", context.getPackageName()));
LogUtils.d(TAG, String.format("action %s", action));
String sos = intent.getStringExtra("sos");
LogUtils.d(TAG, String.format("sos %s", sos));
if (sos != null && sos.equals("SOS")) {
String sosCalssType = intent.getStringExtra("sosCalssType");
LogUtils.d(TAG, String.format("sosCalssType %s", sosCalssType));
if (sosCalssType != null && sosCalssType.equals("Service")) {
String sosPackage = intent.getStringExtra("sosPackage");
LogUtils.d(TAG, String.format("sosPackage %s", sosPackage));
String sosClassName = intent.getStringExtra("sosClassName");
LogUtils.d(TAG, String.format("sosClassName %s", sosClassName));
Intent intentService = new Intent();
intentService.setComponent(new ComponentName(sosPackage, sosClassName));
context.startService(intentService);
LogUtils.d(TAG, String.format("context.startService(intentService);"));
}
}
} else {
LogUtils.d(TAG, String.format("action %s", action));
}
}
}