在mainactivity中更改值时更新小部件textview

zy1mlcev  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(245)

这是appwidget类。一旦我打开小部件,它就会显示mainactivity中的值。但是当值在mainactivity中更改时。小部件不更新值。我应该如何实现这一点。
主活动具有相同的值。我应该使用意图还是有其他方法来做它。

public class NewAppWidget extends AppWidgetProvider {

        static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                    int appWidgetId) {

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
      static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                    int appWidgetId) {

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

            views.setTextViewText(R.id.line, "");

            double latitude = Double.parseDouble(AppSettingsPref.getStringValue(context, AppSettingsPref.LATITUDE_KEY, "0"));
            double longitude = Double.parseDouble(AppSettingsPref.getStringValue(context, AppSettingsPref.LONGITUDE_KEY, "0"));
            PrayTime prayers = new PrayTime();

            Date now = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            int muslimType = AppSettingsPref.getIntValue(context, AppSettingsPref.USER_MUSLIM_TYPE, 0);

            ArrayList<String> prayerTimes = new ArrayList<>();
            if (muslimType == 0) {
                prayers.setTimeFormat(prayers.TIME_12);
                prayers.setCalcMethod(prayers.KARACHI);
                prayers.setAsrJuristic(prayers.SHAFII);
                prayerTimes = prayers.getPrayerTimes(cal, latitude, longitude, prayers.getTimeZone1());
            } else {
                prayers.setTimeFormat(prayers.TIME_12);
                prayers.setCalcMethod(prayers.JAFARI);
                prayers.setAsrJuristic(prayers.SHAFII);
                prayerTimes = prayers.getPrayerTimes(cal, latitude, longitude, prayers.getTimeZone1());
            }
            prayerTimes.add(prayerTimes.get(0));

            ArrayList<String> prayerNames = prayers.getTimeNames();

            views.setTextViewText(R.id.fajrNamazTime, prayerTimes.get(0));
            views.setTextViewText(R.id.dhuhrNamazTime, prayerTimes.get(2));
            views.setTextViewText(R.id.asrNamazTime, prayerTimes.get(3));
            views.setTextViewText(R.id.magribNamazTime, prayerTimes.get(5));
            views.setTextViewText(R.id.ishNamazTime, prayerTimes.get(6));

new\u app\u widget.xml这是一个xml文件。

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/section_id"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    style="@style/AppWidgetSection"
    android:background="@drawable/radius"
    android:padding="15dp">

<TextView
        android:id="@+id/fajrNamazTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />
<TextView
        android:id="@+id/dhuhrNamazTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />
<TextView
        android:id="@+id/asrNamazTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />
<TextView
        android:id="@+id/magribNamazTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />
<TextView
        android:id="@+id/ishanamaztime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />

</RelativeLayout>

new\u app\u widget\u info.xml这是新的app widget info xml。

<TextView
        android:id="@+id/fajrNamazTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="5:42 am"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        />

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题