应用小部件从服务器获取数据

时间:2015-01-16 12:02:38

标签: android widget jsoup

我开发了一个小部件,它将从服务器获取数据并进行一些修改并在小部件中显示。

我有一个ALARM管理器,每10秒启动一次服务,服务使用异步任务和显示器获取数据。

它消耗了大量的CPU使用量。

如果我可以更改代码设计并使小部件更有效,请提供帮助。

我使用Jsoup来连接和获取数据。

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
     Log.d("Widget : " , "Inside onUpdate widget");
    // To prevent any ANR timeouts, we perform the update in a service
     final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  

     final Calendar TIME = Calendar.getInstance();  
     TIME.set(Calendar.MINUTE, 0);  
     TIME.set(Calendar.SECOND, 0);  
     TIME.set(Calendar.MILLISECOND, 0);  

     final Intent i = new Intent(context, UpdateService.class);  

     if (service == null)  
     {  
         service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);  
     }  

     m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 100 * 60, service);  

}

public static class UpdateService extends Service {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
       // System.out.println("Inside config change ");

        enableControls(this);

    }
    @Override
    public void onStart(Intent intent, int startId) {
        // Build the widget update for today
        mContext = this;

        Log.d("Widget : " , "Service started");

        enableControls(this);

    }

public static void enableControls(Context context){          RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_status);

     Intent active = new Intent(context, MyWidgetProvider.class);
     active.setAction(ACTION_WIDGET_REFRESH);
     active.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);

     pushWidgetUpdate(context, remoteViews);
     fetchStatus();

}

public static void fetchStatus(){

            new AsyncTask<String, Void, String>() {
                @Override
                protected String doInBackground(String... urls) {
                try {


                        if(getWiFiStatus(mContext) == true ){
                        boolean getRedirectedURL = false;

                        String url,finalURL;
                        finalURL = "http://mobile.server.status";
                        Document doc =  Jsoup.connect(finalURL).get(); 

                        while(getRedirectedURL == false){
                                Log.d(LOG_TAG,"inside while loop ---- : " + getRedirectedURL);
                                doc =  Jsoup.connect(finalURL).get();
                                Log.d(LOG_TAG,"Complete doc: " + doc);
                              // System.out.println("Found the doc : " +doc.toString().contains("status"));

                               // Log.d(LOG_TAG,"Whole data third :"+doc);

                               // System.out.println("Printing redirected URL " + (docHead.select("meta")));
                                if(doc.toString().contains("status")){
                                   getRedirectedURL = true;
                                   break;
                                }
                                else
                                    getRedirectedURL = false;
                                Elements docHead = doc.select("meta");
                                for(Element emt :docHead) {
                                     url =null;
                                     //Log.d(LOG_TAG,"Value boolean :"+emt.getElementsByAttribute("http-equiv").toString().toLowerCase().contains("url"));
                                    if(emt.getElementsByAttribute("http-equiv").toString().toLowerCase().contains("url")  ){
                                         //getRedirectedURL = true;

                                         url = emt.getElementsByAttribute("http-equiv").toString().toLowerCase().split("url=")[1].toString();

                                    if (emt.getElementsByAttribute("http-equiv").toString().toLowerCase().contains("http://")){
                                            finalURL = null;
                                            finalURL = url.split("\"")[0].toString();
                                    }
                                    else
                                        finalURL = finalURL + url.split("\"")[0].toString();
                                          //Log.d(LOG_TAG,"Final URL :"+finalURL);
                                    }
                                    // else
                                        // getRedirectedURL = false;
                               }
                        }
                        Log.d(LOG_TAG,"Final URL :"+finalURL);
                        doc =  Jsoup.connect(finalURL).get();

                        Document noFramesDoc = Jsoup.parseBodyFragment(doc.toString());


                        }
                        else{
                            refreshStatus(mContext);
                        }
                    } catch (UnknownHostException exception){
                        System.out.println("Inside unknown host : ");

                    }
                    catch (Exception e) {
                        Log.d(LOG_TAG,"other Exception found :"+e.toString());
                        e.printStackTrace();

                    }

                    return null;
                }
                @Override
                protected void onPostExecute(String result) {
                    if(mContext != null)


                }
                @Override
                protected void onCancelled() {

                }

            }.execute(); 
    }

0 个答案:

没有答案