亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Widget的一個小問題。

我并不想做一個時鐘的小組件,我想把網上的一句話來代替時鐘文本,所以我寫好了一個類,用于爬網上的一些句子,但是我在Service里面實例化來取這個字符串的時候并沒有成功,很是奇怪,我想問一下為什么呢?時鐘的組件運行是沒有問題的,而一句話運行的時候沒有結果。

下面是封裝的網絡請求類:

package?com.example.ssc.oneget;

import?android.util.Log;

import?org.jsoup.Jsoup;
import?org.jsoup.nodes.Document;
import?org.jsoup.select.Elements;

import?java.io.IOException;

import?okhttp3.Call;
import?okhttp3.Callback;
import?okhttp3.OkHttpClient;
import?okhttp3.Request;
import?okhttp3.Response;

/**
?*?Created?by?SSC?on?2017/9/16.
?*/

public?class?GetContent?{
????String?content;
????public?String?getContentValue(String?url)?{
????????OkHttpClient?mClient?=?new?OkHttpClient();
????????final?Request?request?=?new?Request.Builder()
????????????????.url(url)
//????????????????.addHeader("User-Agent","Mozilla/5.0?(Windows?NT?10.0;?WOW64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/55.0.2883.87?Safari/537.36")
//????????????????.addHeader("Cookie","ICIBA_HUAYI_COOKIE=1;?UM_distinctid=15e88d42c6e4a5-032081ce67094e-5d4e211f-1fa400-15e88d42c6f3b8;?PHPSESSID=5su1hqc2rqfmm027eqou523pp0;?testnews_iciba_user=15;?CNZZDATA1256573685=1319828842-1505532611-http%253A%252F%252Fwww.so.com%252F%7C1505532611")
//????????????????.addHeader("Cache-Control","max-age=0")
????????????????.build();
????????Call?call?=?mClient.newCall(request);
????????call.enqueue(new?Callback()?{
????????????@Override
????????????public?void?onFailure(Call?call,?IOException?e)?{

????????????}

????????????@Override
????????????public?void?onResponse(Call?call,?Response?response)?throws?IOException?{
????????????????String?result?=?response.body().string();
????????????????Log.i("2333---result",?result);
????????????????Document?document?=?Jsoup.parse(result);
????????????????Elements?elements?=?document.select("strong[style*=\"color:?#0b5394;\"]");
????????????????int?size?=?elements.size();
????????????????content?=?elements.get(0).text().toString();
????????????????Log.i("2333---content+?size",?content?+?"?"?+?size);
//????????????????for?(int?i?=?0;?i?<size?;?i++)?{
//
//????????????????}
????????????}
????????});
????????return?content;
????}
}

下面是Service類:

package?com.example.ssc.oneget;

import?android.app.Service;
import?android.appwidget.AppWidgetManager;
import?android.content.ComponentName;
import?android.content.Intent;
import?android.os.IBinder;
import?android.support.annotation.Nullable;
import?android.widget.RemoteViews;

import?java.text.SimpleDateFormat;
import?java.util.Date;
import?java.util.Timer;
import?java.util.TimerTask;

/**
?*?Created?by?SSC?on?2017/9/16.
?*/

public?class?MyService?extends?Service?{
????private?Timer?mTimer;
????private?SimpleDateFormat?mFormat?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
????@Nullable
????@Override
????public?IBinder?onBind(Intent?intent)?{
????????return?null;
????}

????@Override
????public?void?onCreate()?{
????????super.onCreate();
????????mTimer?=?new?Timer();
????????mTimer.schedule(new?TimerTask()?{
????????????@Override
????????????public?void?run()?{
????????????????updateViews();
????????????}
????????},0,1000);
????}

????private?void?updateViews()?{
????????//時鐘小組件
????????String?time?=?mFormat.format(new?Date());
????????RemoteViews?remoteViews?=?new?RemoteViews(getPackageName(),?R.layout.widget);
????????remoteViews.setTextViewText(R.id.textView1,?time);
????????AppWidgetManager?manager?=?AppWidgetManager.getInstance(getApplicationContext());
????????ComponentName?componentName?=?new?ComponentName(getApplicationContext(),?WidGetProvider.class);
????????manager.updateAppWidget(componentName,remoteViews);
//????????//每日一句
//????????GetContent?getContent?=?new?GetContent();
//????????int?num?=?(int)?(Math.random()?*?300?+?1);
//????????String?url?=?"http://www.dailyenglishquote.com/page/"?+?num?+?"/";
//????????String?content?=?getContent.getContentValue(url);
//????????RemoteViews?remoteViews?=?new?RemoteViews(getPackageName(),?R.layout.widget);
//????????remoteViews.setTextViewText(R.id.textView1,?content);
//????????AppWidgetManager?manager?=?AppWidgetManager.getInstance(getApplicationContext());
//????????ComponentName?componentName?=?new?ComponentName(getApplicationContext(),?WidGetProvider.class);
//????????manager.updateAppWidget(componentName,remoteViews);
????}
????@Override
????public?void?onDestroy()?{
????????super.onDestroy();
????????mTimer?=?null;
????}
}

注釋掉的就是寫一句話來代替時鐘文本的。

謝謝。

正在回答

2 回答

一秒鐘還來不及從網上獲取那就話的內容,而且你用的是okhttp的異步執行,所以并不是先網絡獲取數據再更新組件,而是更新組件和網絡獲取會同時進行,所以你應該在GetContent類里用Handler來接收獲取到的數據并更新組件。再service里的更新頻率降低點,1秒一句話你看都看不過來

0 回復 有任何疑惑可以回復我~
#1

Omooo 提問者

非常感謝!
2018-05-30 回復 有任何疑惑可以回復我~

Service注釋的應該是下面這樣的,打錯了:) 問題還是沒解決。。

//每日一句
GetContent?getContent?=?new?GetContent();
int?num?=?(int)?(Math.random()?*?300?+?1);
String?url?=?"http://www.dailyenglishquote.com/page/"?+?num?+?"/";
String?content?=?getContent.getContentValue(url);
RemoteViews?remoteViews?=?new?RemoteViews(getPackageName(),?R.layout.widget);
remoteViews.setTextViewText(R.id.textView1,?content);
AppWidgetManager?manager?=?AppWidgetManager.getInstance(getApplicationContext());
ComponentName?componentName?=?new?ComponentName(getApplicationContext(),?WidGetProvider.class);
manager.updateAppWidget(componentName,remoteViews);


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android-Widget桌面小組件
  • 參與學習       29578    人
  • 解答問題       32    個

Android-Widget桌面小組件的使用,帶你實現多個小組件功能

進入課程

Widget的一個小問題。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號