У меня проблема с закрытием моей виджеты. Я читал, что мне, возможно, нужно сделать расширение AsyncTask
, но не может на всю жизнь выяснить, как это сделать с AppWidgetProvider
. Я начал использовать учебник, в котором использовались устаревшие функции, а затем понял, что мне нужно использовать HttpURLConnection
, теперь он закроется.Android Widget HttpURLConnection Forceclosing
Этот виджет предназначен только для меня, чтобы следить за температурой котла на работе, и я отнюдь не разработчик Android. Любая помощь серьезно оценена.
public class BoilerTemp extends AppWidgetProvider {
String name;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 10000);
select();
}
public void select()
{
try{
URL url = new URL("http://www.someurl.com/temp/select.php");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
name = line;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.boiler_temp);
thisWidget = new ComponentName(context, BoilerTemp.class);
}
@Override
public void run() {
remoteViews.setTextViewText(R.id.appwidget_text, "TEMP = " + name + " - " + format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
@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
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.boiler_temp);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
обновление Я нашел этот код сделал приложение работает, но в то же время серьезно неодобрительно.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Любые лучшие решения?
Использование LogCat для изучения трассировки стека Java, связанный с грохотом: https://stackoverflow.com/questions/23353173/К сожалению, myapp-has-stop-how-can-i-solve-this – CommonsWare
К сожалению, LogCat пуст. Я использую Android Stuido – Jptalon
Наконец-то я получил что-то в окне LogCat. java.lang.RuntimeException: невозможно запустить приемник – Jptalon