Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. This is my WIdgetprover-Class:
  2.  
  3.     public class MyWidgetProvider2 extends AppWidgetProvider {
  4.  
  5.      private PendingIntent service = null;  
  6.      
  7.         @Override  
  8.         public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)  
  9.         {  
  10.             final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
  11.      
  12.             final Calendar TIME = Calendar.getInstance();  
  13.             TIME.set(Calendar.MINUTE, 0);  
  14.             TIME.set(Calendar.SECOND, 0);  
  15.             TIME.set(Calendar.MILLISECOND, 0);  
  16.      
  17.             final Intent i = new Intent(context.getApplicationContext(), UpdateWidgetService2.class);  
  18.      
  19.             ComponentName thisWidget = new ComponentName(context,
  20.                     MyWidgetProvider2.class);
  21.             int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
  22.  
  23.             i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
  24.            
  25.             if (service == null)  
  26.             {  
  27.                 service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);  
  28.             }  
  29.      
  30.             m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 60 * 30, service);  
  31.         }  
  32.      
  33.         @Override  
  34.         public void onDisabled(Context context)  
  35.         {  
  36.             final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
  37.      
  38.             m.cancel(service);  
  39.         }  
  40.     }  
  41.  
  42. And this the service that is getting called:
  43.  
  44.  
  45.     package com.skyworxx.htwdd;
  46.  
  47. import java.io.BufferedReader;
  48. import java.io.InputStream;
  49. import java.io.InputStreamReader;
  50. import java.net.HttpURLConnection;
  51. import java.net.MalformedURLException;
  52. import java.net.URL;
  53. import java.net.URLConnection;
  54. import java.util.Calendar;
  55. import java.util.Locale;
  56. import java.util.Random;
  57.  
  58.  
  59.  
  60.  
  61. import android.app.PendingIntent;
  62. import android.app.Service;
  63. import android.appwidget.AppWidgetManager;
  64. import android.content.ComponentName;
  65. import android.content.Intent;
  66. import android.graphics.Bitmap;
  67. import android.graphics.BitmapFactory;
  68. import android.net.Uri;
  69. import android.os.AsyncTask;
  70. import android.os.Bundle;
  71. import android.os.IBinder;
  72. import android.util.Log;
  73. import android.view.View;
  74. import android.widget.ImageView;
  75. import android.widget.RemoteViews;
  76. import android.widget.TextView;
  77. import android.widget.Toast;
  78.  
  79. public class UpdateWidgetService2 extends Service {
  80.     private static final String LOG = "example";
  81.  
  82.     @Override
  83.     public void onStart(Intent intent, int startId) {
  84.         Log.i(LOG, "Called");
  85.    
  86.  
  87.         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
  88.                 .getApplicationContext());
  89.  
  90.         int[] allWidgetIds = intent
  91.                 .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
  92.  
  93.         ComponentName thisWidget = new ComponentName(getApplicationContext(),
  94.                 MyWidgetProvider2.class);
  95.         int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
  96.         Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
  97.         Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
  98.  
  99.         for (int widgetId : allWidgetIds) {
  100.             // Create some random data
  101.             int number = (new Random().nextInt(100));
  102.  
  103.             RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(),
  104.                     R.layout.widget_layout2);
  105.             Log.w("WidgetExample", String.valueOf(number));
  106.            
  107.             worker w = new worker();
  108.                 w.execute("1", "2");
  109.                
  110.        
  111.             appWidgetManager.updateAppWidget(widgetId, remoteViews);
  112.            
  113.            
  114.         }
  115.         stopSelf();
  116.  
  117.         super.onStart(intent, startId);
  118.     }
  119.  
  120.        
  121.    
  122.    
  123.     private class worker extends AsyncTask<String, Void, mahlzeit[]> {
  124.         @Override
  125.         protected mahlzeit[] doInBackground(String... params) {
  126.        
  127.    
  128.        
  129.            
  130.              mahlzeit[] meals=new mahlzeit[8];
  131.                
  132.            
  133.    
  134.             URL url;
  135.             try {
  136.                
  137. //doing the network stuff and save all the data in meals
  138.              
  139.            
  140.          
  141.             } catch (Exception e) {
  142.                
  143.                 e.printStackTrace();
  144.                
  145.             }
  146.            
  147.            
  148.             return meals;
  149.    
  150.         }
  151.        
  152.         @Override
  153.         protected void onPostExecute(final mahlzeit[] meals) {
  154.            
  155.            
  156.              
  157.              
  158.          remoteViews.setTextViewText(R.id.textView1,meals[0].getPreis());  
  159.  
  160.                  
  161.         }
  162.    
  163.    
  164.     }
  165.    
  166.    
  167.    
  168.    
  169.     @Override
  170.     public IBinder onBind(Intent intent) {
  171.         return null;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement