关于获取IP到小部件的咨询

时间:2013-07-08 12:27:44

标签: android

我有需要显示IP的小部件

我有这个用于显示IP

public ShowIP()
{
try
        {
         WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
            WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

            int myIp = Math.abs(myWifiInfo.getIpAddress());

            int intMyIp3 = myIp/0x1000000;
            int intMyIp3mod = myIp%0x1000000;

            int intMyIp2 = intMyIp3mod/0x10000;
            int intMyIp2mod = intMyIp3mod%0x10000;

            int intMyIp1 = intMyIp2mod/0x100;
            int intMyIp0 = intMyIp2mod%0x100;

            IP = String.valueOf(intMyIp0) + "." + String.valueOf(intMyIp1) + "." + String.valueOf(intMyIp2) + "." + String.valueOf(intMyIp3); //===V
        }
        catch (Exception ex)
        {
            IP = "Error Network";
        }
    }

在我的小部件中,我尝试像这样调用ShowIP():

public class HelloWidget extends AppWidgetProvider {

    public String IP;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    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.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
    }

    @Override
    public void run() {
                ShowIP();
        remoteViews.setTextViewText(R.id.widget_textview, IP);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }


    @SuppressWarnings("unused")
    public  void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    }

}

getSystemService未识别单词

的问题

怎么去?

感谢

0 个答案:

没有答案