0

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

Melvin Lai
  • 861
  • 3
  • 17
  • 35
  • widgets are basically broadcast receivers. They receive a context in parameter. But if you need internet access from there, you'd best start a service. – njzk2 Oct 19 '12 at 08:42

1 Answers1

2

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/service/receiver

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Ah, thanks for the reply. When you say use service to do the httprequest, do I need to extend it? Since I already extends **AppWidgetProvider**, can I still extends **Service** somewhere within the code? I have never attempted this before and would like to clarify before I test it out. – Melvin Lai Oct 23 '12 at 01:35