Creating an android widget to implement location listener

Put your problem here if it does not fit any of the other categories.

Creating an android widget to implement location listener

Postby android_newbie2009 » Sun Nov 22, 2009 5:51 pm

I'm creating an android widget that needs to perform an httppost to a php file when the location changes. The widget is supposed to update the screen with the results of the httppost. The widget does not appear to update at all. Can someone please help? Here's my code:

(I've removed the import statements)

I'd also like to modify the background color to indicate my status. Does anyone know of any examples of this?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Widget extends AppWidgetProvider implements Runnable, LocationListener {
  2.  
  3.  
  4.  
  5.         private LocationManager mgr;
  6.  
  7.         private static String userName = "uid";
  8.  
  9.         private String myStatus = "Waiting...";
  10.  
  11.         private String queryString;
  12.  
  13.        
  14.  
  15.         private RemoteViews updateViews = null;
  16.  
  17.        
  18.  
  19.         public static final int MESSAGE_OK = 0x01;
  20.  
  21.         public static final int MESSAGE_ERROR = 0x02;
  22.  
  23.        
  24.  
  25.         @Override
  26.  
  27.         public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  28.  
  29.                
  30.  
  31.                 // Change the text in the widget
  32.  
  33.                 updateViews = new RemoteViews(context.getPackageName(), R.layout.main);
  34.  
  35.                 mgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  36.  
  37.          
  38.  
  39.             getStatus(userName);
  40.  
  41.             updateViews.setTextViewText(R.id.text, myStatus);
  42.  
  43.             appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
  44.  
  45.            super.onUpdate(context, appWidgetManager, appWidgetIds);
  46.  
  47.         }
  48.  
  49.        
  50.  
  51.         public void onLocationChanged(Location arg0) {
  52.  
  53.                 getStatus(userName);
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.         public void onProviderDisabled(String provider) {
  60.  
  61.                 // TODO Auto-generated method stub
  62.  
  63.         }
  64.  
  65.  
  66.  
  67.         public void onProviderEnabled(String provider) {
  68.  
  69.                 // TODO Auto-generated method stub
  70.  
  71.                
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.         public void onStatusChanged(String provider, int status, Bundle extras) {
  78.  
  79.                 // TODO Auto-generated method stub
  80.  
  81.                
  82.  
  83.         }
  84.  
  85.        
  86.  
  87.         private void  getStatus(String userName)
  88.  
  89.         {
  90.  
  91.             queryString = "http://phplocation/file.php?";
  92.  
  93.                 queryString = queryString + "&user=" + userName;
  94.  
  95.        
  96.  
  97.                 // Create a new thread to run the httpget
  98.  
  99.                 Thread thread = new Thread(this);
  100.  
  101.                 thread.start();
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         public void run() {
  108.  
  109.                 Message msg = new Message();
  110.  
  111.                 msg.what = MESSAGE_OK;
  112.  
  113.                 HttpClient httpclient = new DefaultHttpClient();
  114.  
  115.         HttpPost httppost = new HttpPost(queryString);
  116.  
  117.         ResponseHandler<String> responseHandler = new BasicResponseHandler();
  118.  
  119.         try {
  120.  
  121.                         myStatus = httpclient.execute(httppost, responseHandler);
  122.  
  123.                         if(myStatus == null){
  124.  
  125.                                 msg.what = MESSAGE_ERROR;
  126.  
  127.                         }
  128.  
  129.                 } catch (ClientProtocolException e) {
  130.  
  131.                         // TODO Auto-generated catch block
  132.  
  133.                         e.printStackTrace();
  134.  
  135.                 } catch (IOException e) {
  136.  
  137.                         // TODO Auto-generated catch block
  138.  
  139.                         e.printStackTrace();
  140.  
  141.                 }
  142.  
  143.         // When HttpClient instance is no longer needed,
  144.  
  145.         // shut down the connection manager to ensure
  146.  
  147.         // immediate deallocation of all system resources
  148.  
  149.         httpclient.getConnectionManager().shutdown();
  150.  
  151.                 handler.sendMessage(msg);
  152.  
  153.         }
  154.  
  155.        
  156.  
  157.         private Handler handler = new Handler() {      
  158.  
  159.                 //@Override    
  160.  
  161.                 public void handleMessage(Message msg) {      
  162.  
  163.                        updateViews.setTextViewText(R.id.text, myStatus);
  164.  
  165.                     }
  166.  
  167.             };
  168.  
  169. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.      package="org.example.reminder"
  6.  
  7.      android:versionCode="1"
  8.  
  9.      android:versionName="1.0">
  10.  
  11.       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  12.  
  13.       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  14.  
  15.       <uses-permission android:name="android.permission.INTERNET" />
  16.  
  17.       <uses-permission android:name="android.permission.VIBRATE" />  
  18.  
  19.     <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
  20.  
  21.     <!-- Broadcast Receiver that will process AppWidget updates -->
  22.  
  23.     <receiver android:name=".Widget"
  24.  
  25.            android:label="@string/widget_name">
  26.  
  27.         <intent-filter>
  28.  
  29.             <action android:name=
  30.  
  31.            "android.appwidget.action.APPWIDGET_UPDATE" />
  32.  
  33.         </intent-filter>
  34.  
  35.         <meta-data android:name="android.appwidget.provider"
  36.  
  37.            android:resource="@xml/widget" />
  38.  
  39.     </receiver>
  40.  
  41.  
  42.  
  43.     <shape xmlns:android="http://schemas.android.com/apk/res/android">
  44.  
  45.         <gradient
  46.  
  47.            android:startColor="#FFFFFF"
  48.  
  49.            android:endColor="#808080"
  50.  
  51.            android:angle="270" />
  52.  
  53.     </shape>
  54.  
  55.  
  56.  
  57.     </application>
  58.  
  59.     <uses-sdk android:minSdkVersion="3" />
  60.  
  61.  
  62.  
  63. </manifest>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4
android_newbie2009
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 13, 2009 1:51 pm

Top

In response to my own question...

Postby android_newbie2009 » Sun Nov 22, 2009 11:15 pm

I removed the LocationListener based on how widgets are supposed to work and updated the code to the following; however, the widget is not updating:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Widget extends AppWidgetProvider {
  2.  
  3.  
  4.  
  5.         /** Called when the activity is first created. */
  6.  
  7.         private static String userName = "uid";
  8.  
  9.         private RemoteViews updateViews = null;
  10.  
  11.        
  12.  
  13.         private SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy\nHH:mm:ss.SSS" );
  14.  
  15.        
  16.  
  17.         @Override
  18.  
  19.         public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  20.  
  21.                
  22.  
  23.                 // Retrieve and format the current date and time
  24.  
  25.                 getStatus(uid);
  26.  
  27.        
  28.  
  29.                 // Change the text in the widget
  30.  
  31.                 updateViews = new RemoteViews(context.getPackageName(), R.layout.main);
  32.  
  33.                 updateViews.setTextViewText(R.id.text, myStatus);
  34.  
  35.                 appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
  36.  
  37.                 super.onUpdate(context, appWidgetManager, appWidgetIds);
  38.  
  39.         }
  40.  
  41.        
  42.  
  43.         private void  getStatus(String uid)
  44.  
  45.         {
  46.  
  47.                 queryString = "http://myfile.php?";
  48.  
  49.                 HttpClient httpclient = new DefaultHttpClient();
  50.  
  51.                 HttpPost httppost = new HttpPost(queryString);
  52.  
  53.                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
  54.  
  55.                 try {
  56.  
  57.                              getStatus = httpclient.execute(httppost, responseHandler);
  58.  
  59.                         } catch (ClientProtocolException e) {
  60.  
  61.                                 // TODO Auto-generated catch block
  62.  
  63.                                 e.printStackTrace();
  64.  
  65.                         } catch (IOException e) {
  66.  
  67.                                 // TODO Auto-generated catch block
  68.  
  69.                                 e.printStackTrace();
  70.  
  71.                         }
  72.  
  73.                
  74.  
  75.                         String now = formatter.format(new Date());
  76.  
  77.                         myStatus = myStatus + " " + now;       
  78.  
  79.         }
  80.  
  81.  
  82.  
  83.     };
  84.  
  85.            
  86.  
  87. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
android_newbie2009
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 13, 2009 1:51 pm

Dynamic background image change

Postby mbaird » Wed Dec 09, 2009 3:40 am

Has anybody figured this out? Is there a way to update the background image of a widget? I'd really like to be able to apply a color filter.
mbaird
Freshman
Freshman
 
Posts: 4
Joined: Sat Aug 29, 2009 12:18 am

Changing background images

Postby android_newbie2009 » Thu Dec 10, 2009 2:36 am

To change the background images for the widget, we had to create separate images and switch through each via the setImageViewResource command:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 if(image1==true){
  3.  
  4.                      remoteViews.setImageViewResource(R.id.Bulb, R.drawable.yourImage1);
  5.  
  6.                 }                        
  7.  
  8.                          else if(image2r==true)
  9.  
  10.                          {       
  11.  
  12.                                  remoteViews.setImageViewResource(R.id.Bulb, R.drawable.yourImage2);
  13.  
  14.                          }
  15.  
  16.                          else
  17.  
  18.                          {
  19.  
  20.                                  remoteViews.setImageViewResource(R.id.Bulb, R.drawable.yourDefaultImage);
  21.  
  22.                          }
  23.  
  24.                          
  25.  
  26.                          appWidgetManager.updateAppWidget(thisWidget, remoteViews);
  27.  
  28.                          
  29.  
  30.                  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
android_newbie2009
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 13, 2009 1:51 pm

Postby padde » Thu Dec 10, 2009 8:43 am

Use a service to listen for location updates etc. and just
control this service with your widget.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 20 guests