Thomas Chef 2 年之前
父節點
當前提交
99eab37b84

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -20,6 +20,7 @@
         android:theme="@style/Theme.JourneyGPSTracker"
         android:usesCleartextTraffic="true"
         tools:targetApi="31">
+
         <receiver
             android:name=".HomeTempAppWidget"
             android:exported="false">

+ 21 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/HomeTempAppWidget.kt

@@ -2,16 +2,19 @@ package com.flacksta.chef.journeygpstracker
 
 import android.appwidget.AppWidgetManager
 import android.appwidget.AppWidgetProvider
+import android.content.ComponentName
 import android.content.Context
 import android.content.Intent
 import android.util.Log
 import android.widget.RemoteViews
 
+
 /**
  * Implementation of App Widget functionality.
  */
 class HomeTempAppWidget : AppWidgetProvider() {
 
+    val ACTION_UPDATE = "com.flacksta.chef.journeygpstracker.action.UPDATE"
     var updateCnt: Int = 0
 
     override fun onUpdate(
@@ -21,24 +24,41 @@ class HomeTempAppWidget : AppWidgetProvider() {
     ) {
         updateCnt++
         Log.i("KALLE", "onUpdate cnt:$updateCnt")
-        // There may be multiple widgets active, so update all of them
 
+        // There may be multiple widgets active, so update all of them
         for (appWidgetId in appWidgetIds) {
             updateAppWidget(context, appWidgetManager, appWidgetId, updateCnt)
         }
     }
 
+    fun onUpdate( context: Context ) {
+        val appWidgetManager = AppWidgetManager.getInstance(context)
+
+        val thisAppWidgetComponentName = ComponentName(context.packageName, javaClass.name)
+        val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName)
+        onUpdate(context, appWidgetManager, appWidgetIds)
+    }
+
     override fun onEnabled(context: Context) {
         // Enter relevant functionality for when the first widget is created
+        Log.d("KALLE", "onEnabled()");
     }
 
     override fun onDisabled(context: Context) {
         // Enter relevant functionality for when the last widget is disabled
+        Log.d("KALLE", "onDisabled()");
     }
 
     override fun onReceive(context: Context?, intent: Intent?) {
         super.onReceive(context, intent)
         Log.d("KALLE", "onReceive()" + intent?.action);
+        if (intent != null) {
+            if (ACTION_UPDATE.equals(intent.getAction())) {
+                if (context != null) {
+                    onUpdate(context)
+                };
+            } else super.onReceive(context, intent)
+        };
     }
 }
 

+ 31 - 0
app/src/main/java/com/flacksta/chef/journeygpstracker/WidUtils.kt

@@ -0,0 +1,31 @@
+package com.flacksta.chef.journeygpstracker
+
+import android.app.AlarmManager
+import android.app.PendingIntent
+import android.content.Context
+import android.content.Intent
+import android.preference.PreferenceManager
+
+
+class WidUtils {/*
+    fun scheduleUpdate(context: Context) {
+        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager
+        val pendingIntent = getAlarmIntent(context) //PendingIntent.getService(context, requestId, intent, PendingIntent.FLAG_NO_CREATE)
+
+        val intervalMillis = (interval!!.toInt() * 60 * 1000).toLong()
+        val pi = getAlarmIntent(context)
+        am.cancel(pi)
+        am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalMillis, pi)
+    }
+
+    private fun getAlarmIntent(context: Context): PendingIntent {
+        val intent = Intent(context, HomeTempAppWidget::class.java)
+        intent.action = HomeTempAppWidget.ACTION_UPDATE
+        return PendingIntent.getBroadcast(context, 0, intent, 0)
+    }
+
+    fun clearUpdate(context: Context) {
+        val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
+        am.cancel(getAlarmIntent(context))
+    }*/
+}

+ 3 - 3
app/src/main/res/xml/home_temp_app_widget_info.xml

@@ -3,12 +3,12 @@
     android:description="@string/app_widget_description"
     android:initialKeyguardLayout="@layout/home_temp_app_widget"
     android:initialLayout="@layout/home_temp_app_widget"
-    android:minWidth="80dp"
-    android:minHeight="80dp"
+    android:minWidth="146dp"
+    android:minHeight="72dp"
     android:previewImage="@drawable/example_appwidget_preview"
 
     android:previewLayout="@layout/home_temp_app_widget"
     android:targetCellWidth="2"
     android:targetCellHeight="2"
-    android:updatePeriodMillis="5000"
+    android:updatePeriodMillis="1800000"
     android:widgetCategory="home_screen" />