Android的Wigets不能使用自定义DefaultHttpClient [英] Android Wigets can't use custom DefaultHttpClient

查看:147
本文介绍了Android的Wigets不能使用自定义DefaultHttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是有点混乱,因为我不知道什么是适当的方式命名。我刚开始学小部件编程和大致的想法得到了如何做一个简单的小部件。现在我想用我的实际应用程序,做了几件事情:


  1. 使用一个IP地址的ping网络服务器(检查每个服务器的积极性)

  2. Web服务器要求证书身份验证

实际的应用程序已经没有问题的工作,我只想为我侦测到服务器1做一个小部件。我用了一个自定义的DefaultHttpClient, MyHttpClient 的类,它确实我所有的证书认证。我怎么用这个,的 MyHttpClient 的类是通过键入

  DefaultHttpClient客户端=新MyHttpClient(getApplicationContext());

如果我一个的活动类中有它,它才会起作用。它不会在 AppWidgetProvider 类内工作。它给人的错误是:


  

该方法getApplicationContext()是未定义的类型
  SystemWidget.ParseLoyalty SystemWidget.java



解决方案

一个小部件是基本上只是一个观点而已,所以它不能从任何请求数据。如果您想更新的部件psented数据$ P $您可以使用服务做HTT prequest。然后,您可以通过广播自己的行动意图和处理,在你的AppWidgetProvider的onRecieve或者你可以直接更新部件发送数据到小部件。

当然,如果你的活动有效时,它可以更新部件为好。

查看更多在这里:
https://developer.android.com/guide/topics/appwidgets/index.html

的服务是从微件不同。这是一个不同的一块code,你可以执行到DOS青梅行动。一旦你执行的服务,您可以使用检索为我的回答上面所讨论的更新您的插件的数据。下面是关于Android服务教程:

https://developer.android.com/guide/components/services.html

另请参阅此线程,如果你想直接从您的活动更新您的小部件:
<一href=\"http://stackoverflow.com/questions/3455123/programmatically-update-widget-from-activity\">Programmatically从活动更新窗口小部件?

The title is a little confusing as I do not know what is the appropriate way to name it. I just started to learn widget programming and roughly got the idea how to make a simple widget. Now I want to use it on my actual application which does a few things:

  1. Uses an IP Address to ping web servers (to check activeness of each server)
  2. Web servers requires certificate authentication

The actual application is already working with no problems, I just wish to do a widget for 1 of the servers I am pinging to. I used a custom DefaultHttpClient, MyHttpClient class, which does all my certificate authentications. How I use this, MyHttpClient class is by typing

DefaultHttpClient client = new MyHttpClient(getApplicationContext());

And it only works if I have it within an Activity class. It does not work within the AppWidgetProvider class. The error it gives is :

The method getApplicationContext() is undefined for the type SystemWidget.ParseLoyalty SystemWidget.java

解决方案

A widget is basically just a view and nothing more, so it cannot request data from anything. If you want to update the data presented by the widget you can use a service to do the httprequest. Then you can send data to the widget either by broadcasting your own action intent and handling that in the onRecieve of your AppWidgetProvider or you can update the widget directly.

Of course if your activity is active it can update the widget as well.

See more here: https://developer.android.com/guide/topics/appwidgets/index.html

The service is different from the widget. It's a different piece of code that you can execute to dos ome actions. Once you execute the service, you can use the data retrieved to update your widget as discussed in my answer above. Here's a tutorial on android services:

https://developer.android.com/guide/components/services.html

Also see this thread if you wanna update your widget from your activity directly: Programmatically update widget from activity?

这篇关于Android的Wigets不能使用自定义DefaultHttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆