【问题标题】:Initializing object (ex. Textview) on a widget activity在小部件活动上初始化对象(例如 Textview)
【发布时间】:2017-11-18 17:42:15
【问题描述】:

我制作了一个显示 IP 地址、Wi-Fi 名称和其他内容的 Android 应用程序(全部使用异步任务)。现在,我正在制作它的小部件。

代码如下:

package com.example.utente.network;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;


public class NetWidget extends AppWidgetProvider implements asyncTask.ITask{

    static void updateAppWidget(final Context context,
                                AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        TextView ipv4;
        TextView infowifi;

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.net_widget);

        ipv4 = (TextView)views     ********HOT TO INITIALIZE??

        // Setto tutti gli elemnti del widget con eventuali valori di default
        final  WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        // asyncTask runner = new asyncTask(getApplicationContext());
        asyncTask runner = new asyncTask(context.getApplicationContext());
        runner.execute();

        final WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        final int ip = wifiInfo.getIpAddress();
        final String ipAddress = Formatter.formatIpAddress(ip);
        String nomewifi =(wifiInfo.getSSID());

        Log.d("Widget_IP", "doInBackground: " +  ipAddress);
        Log.d("widget_nome_wifi", "doInBackground: " +  nomewifi);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate( final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d("UPDATE WIDGET", "onUpdate: ");

        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @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
    }

    @Override
    public void onProgressValue(int aValue, Integer value) {

    }
}

在这个小部件中,有两个文本视图(目前),但是如何使用 IP 地址和 Wi-Fi 名称(我从异步任务中获得)设置它们的文本?我不知道如何初始化文本视图。我想这应该是正常的行为:

TextView  myTextView;
myTextView = (textView)findViewById(R.id.mytextViewID);

但显然是不对的。

【问题讨论】:

    标签: android widget android-widget


    【解决方案1】:

    您不需要直接初始化 TextView。相反,您可以使用 RemoteView 来更新 TextView。所以,例如:

    views.setTextViewText(R.id.text_view_id, "Text to set");
    

    然后,一旦你调用“updateAppWidget(appWidgetId,views)”,TextView 就会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多