gpt4 book ai didi

java - 计时器小部件卡住

转载 作者:行者123 更新时间:2023-11-29 02:27:52 26 4
gpt4 key购买 nike

我有一个学校计时器应用程序,我尝试制作小部件计时器并且我做到了。但是两天前我注意到一个问题:有时小部件停止工作并且不继续。当我删除并创建另一个小部件时,它会继续工作。但是,这对我来说是个大问题,所以我正在努力解决它。我到处搜索,但找不到任何相关信息。

我该如何解决这个问题?

package com.candydevelopment.okulsayac;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.StrictMode;
import android.util.TypedValue;
import android.widget.ArrayAdapter;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class sayacWidget extends AppWidgetProvider {

private static final String TAG = MainActivity.class.getName();
private static final String FILENAME = "";
TextView txtStartLeftDate, txtOkulText,txttatil15leftdate,txttatil15yazi;
private long startTime,starttime15;
private long diff;
private CountDownTimer mCountDownTimer,mCountDownTimer15;
String acilmaZaman, kapanmaZaman,tatil15;
Context con;
String address = "https://www.candygaminggames.com/okulsayac.php";
InputStream is = null;
String line = null;
String result = null;
String[] data;
ArrayAdapter<String> adapter;
SharedPreferences myPreferences;
SharedPreferences.Editor myEditor;
static final int READ_BLOCK_SIZE = 100;

public void CalculateTime(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
views = new RemoteViews(con.getPackageName(), R.layout.sayac_widget);
try{
acilmaZaman=SaveGet.getValue(con,"Acilma");
kapanmaZaman=SaveGet.getValue(con,"Kapanma");
tatil15=SaveGet.getValue(con,"tatil15");
} catch (Exception ed) {
ed.printStackTrace();
}

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);


Date acilmaTarih;
Date kapanmaTarih;
Date tatil15date;


long milliseconds = 0;
long milliseconds15 = 0;
try {
acilmaTarih = formatter.parse(acilmaZaman);
kapanmaTarih = formatter.parse(kapanmaZaman);
tatil15date = formatter.parse(tatil15);
milliseconds15 = tatil15date.getTime();
milliseconds = acilmaTarih.getTime();

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



diff = milliseconds - startTime;
if (diff < 0) {
try {
kapanmaTarih = formatter.parse(kapanmaZaman);
milliseconds = kapanmaTarih.getTime();
diff = milliseconds - startTime;

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
diff = milliseconds - startTime;

views.setTextViewText(R.id.txtOkulBilgi, "Okulun Kapanmasına Kalan");

apm.updateAppWidget(appWidgetId, views);
}
startTime = System.currentTimeMillis();
mCountDownTimer = new CountDownTimer(milliseconds, 1000 ) {

public void onTick(long millisUntilFinished) {

startTime = startTime - 1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;

String daysLeft = String.valueOf(serverUptimeSeconds / 86400);
String hoursLeft = String.valueOf((serverUptimeSeconds % 86400) / 3600);
String minutesLeft = String.valueOf(((serverUptimeSeconds % 86400) % 3600) / 60);
String secondsLeft = String.valueOf(((serverUptimeSeconds % 86400) % 3600) % 60);
views.setTextViewText(R.id.txtAcilma,daysLeft + " : " + hoursLeft + " : " + minutesLeft + " : " + secondsLeft);
apm.updateAppWidget(WidgetID, views);

}


public void onFinish() {

}
}.start();
starttime15 = System.currentTimeMillis();
mCountDownTimer15 = new CountDownTimer(milliseconds15, 1000) {

public void onTick(long millisUntilFinishedd) {

starttime15 = starttime15 - 1;
Long serverUptimeSeconds15 =
(millisUntilFinishedd - starttime15) / 1000;

String daysLeft = String.valueOf(serverUptimeSeconds15 / 86400);
String hoursLeft = String.valueOf((serverUptimeSeconds15 % 86400) / 3600);
String minutesLeft = String.valueOf(((serverUptimeSeconds15 % 86400) % 3600) / 60);
String secondsLeft = String.valueOf(((serverUptimeSeconds15 % 86400) % 3600) % 60);
views.setTextViewText(R.id.txtAcilma2,daysLeft + " : " + hoursLeft + " : " + minutesLeft + " : " + secondsLeft);
apm.updateAppWidget(appWidgetId, views);

}


public void onFinish() {

}
}.start();
}
int WidgetID;
AppWidgetManager apm;
public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
con = context;
apm = appWidgetManager;
WidgetID = appWidgetId;
try{
acilmaZaman=SaveGet.getValue(con,"Acilma");
kapanmaZaman=SaveGet.getValue(con,"Kapanma");
tatil15=SaveGet.getValue(con,"tatil15");
CalculateTime(context,appWidgetManager,appWidgetId);

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(con,
"Lütfen ilk açılışta internete bağlanın.",
Toast.LENGTH_LONG).show();
}
}

RemoteViews views;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created


}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}



}`

最佳答案

问题是小部件 View 的更新使 AppWidgetProvider. AppWidgetProvider 是一个 BroadcastReceiver,因此它的生命周期非常短(没有 goAsync() 时为 10 秒)。所以在AppWidgetProvider死掉之后,你的views不会被更新。为避免这种行为,请计算日期并从服务更新小部件的 View ,这将解决您的问题。

关于java - 计时器小部件卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com