0
我正在編寫一個應用程序小部件,它從服務器獲取數據並將它們顯示在appwidget中。 的問題是,在沒有互聯網連接,在這一點上,系統更新微件,TextView
文本值復位與android:text="sometext"
將appwidget導致設置值更新爲默認值
它發生這樣的設置好的默認的文本:
- 的Widget放在主屏幕
- 互聯網連接是活躍
- 的Widget成功更新
- 來自服務器的響應的文本安裝在
TextView
- 互聯網連接不活躍
- 系統更新微件的
TextView
重置爲在android:text=""
設置好的,我知道在某個地方我做的並不正確的東西的價值
文件WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider {
public static String LOG_TAG = "MYAPPLOG";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d(LOG_TAG, "onReceive");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.d(LOG_TAG, "onUpdate");
for (int widgetID : appWidgetIds)
{
updateWidget(context, widgetID);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Log.d(LOG_TAG, "onDeleted");
}
public void updateWidget(Context context, int widgetID)
{
context.startService(new Intent(context, UpdatingService.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID));
}
}
文件UpdatingService.java
public class UpdatingService extends IntentService {
public static String LOG_TAG = "MYAPPLOG";
public UpdatingService() {
super("UpdatingService");
}
@Override
protected void onHandleIntent(Intent intent) {
// getting widgetID from intent and other vars
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.initial_layout);
if(isConnected(getApplicationContext()))
{
String response = getServerResponse();
if(response != null)
{
try {
JSONObject JSON = new JSONObject(response);
// get data from server
// ...
// set values to the views
remoteViews.setTextViewText(R.id.textView, someText);
} catch (JSONException e) {
e.printStackTrace();
Log.d(LOG_TAG, "JSONObject failed");
}
}
else
{
// LOG: error connection to server
}
}
else
{
// LOG: No internet connection
}
// updating apwidget (set click action for the some button)
// if not do update then button will not work
Intent someIntent = new Intent(getApplicationContext(), WidgetProvider.class);
someIntent.setAction(WidgetProvider.ACTION_GOTO);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
widgetID, someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.goToLayout, pendingIntent);
AppWidgetManager.getInstance(getApplicationContext().getApplicationContext())
.updateAppWidget(widgetID, remoteViews);
}
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected()) {
return true;
}
return false;
}
public String getServerResponse() {
// using HttpURLConnection
}
}
希望對您有所幫助或稍尖。我寫了幾個小部件,都有這個問題。非常感謝您的關注。
好吧,我在啓動將屬性設置爲textview但仍不起作用的服務代碼之前,粘貼了'updateWidget(Context context,int widgetID)'。每當系統更新沒有互聯網連接的小部件時,即使這些值是由前一次的服務設置的,它也會從xml中設置值。 –
您正在服務中創建應用程序小部件的UI,即RemoteViews remoteViews = new RemoteViews(getApplicationContext()。getPackageName(), R.layout.initial_layout);'。這是「重置」UI的代碼。所以,不要更新'updateWidget(Context context,int widgetID)'方法,而是用'// LOG:沒有互聯網連接'來更新'Service'中的ELSE分支。在那裏,您將設置應用程序小部件的文本爲您保存的值。 – shelll
可能我只需要在一個服務中創建UI('RemoteViews remoteViews = new RemoteViews'),只能使用這個'remoteViews'(當我需要'remoteViews'時不要創建一個)? –