0

我在使用我的Widget應用程序時遇到了強制關閉問題。我讀到,我可能需要做一個擴展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); 

有沒有更好的解決方案?

+1

使用logcat的檢查與碰撞相關的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173 /不幸-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

不幸的是,LogCat是空的。我正在使用Android Stuido – Jptalon

+0

我終於在LogCat窗口中找到了一些東西。 java.lang.RuntimeException:無法啓動接收器 – Jptalon

回答

0

試試這個代碼(它從URL解析XML)。

調用

parser = new XMLParser(); 
parser.execute(context); 

XML解析器類

public class XMLParser extends AsyncTask { 

    private Exception exception; 

    @Override 
    // Parse XML 
    protected Object doInBackground(Object[] objects) { 
     Context context = (Context) objects[0]; 

     HttpURLConnection connection; 
     InputStream inputStream; 
     try { 
      URL input = new URL("http://...."); 
      connection = (HttpURLConnection) input.openConnection(); 
     } catch (IOException e) { 
      exception = e; 
      ... 
      return context; 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
     } 

     try { 

      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      XmlPullParser parser = factory.newPullParser(); 

      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("Accept", "application/xml"); 
      inputStream = connection.getInputStream(); 
      parser.setInput(inputStream, "UTF-8"); 
      ... 
    } 

    // Parsing done 
    protected void onPostExecute(Object val) { 
     if (exception != null) { 
      sentIntent((Context) val, false); 
      exception.printStackTrace(); 
     } else { 
      if (val != null) { 
       sentIntent((Context) val, true); 
      } 
     } 
    } 

    // Sent intent when parsing done 
    private void sentIntent(Context context, final boolean extra) { 
     Intent intent = new Intent(context, BoilerTemp.class); 
     intent.setAction(Constants.ACTION_XML_PARSED); 
     context.sendBroadcast(intent); 
    } 
} 

}