João Felipe Santos avatar João Felipe Santos committed 548d7af

Adicionando widget, não está atualizando o texto corretamente.

Comments (0)

Files changed (7)

AndroidManifest.xml

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-
+		
+		<receiver android:name="CitaDroidWidgetProvider" >
+    		<intent-filter>
+        		<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+    		</intent-filter>
+    		<meta-data android:name="android.appwidget.provider"
+               android:resource="@xml/citadroid_widget_info" />
+		</receiver>
+		
     </application>
 </manifest>

res/layout/citadroid_widget.xml

+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+  xmlns:android="http://schemas.android.com/apk/res/android"
+  android:orientation="vertical"
+  android:layout_width="match_parent"
+  android:layout_height="match_parent" android:weightSum="1">
+    <TextView 
+    	android:id="@+id/linktoapp"
+    	android:text="Uhuhuhuhu!"
+    	android:layout_width="wrap_content"
+    	android:layout_height="wrap_content"
+    	android:background="#FFFFFF">
+    </TextView>
+</LinearLayout>

res/layout/main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="fill_parent" 
-              android:layout_height="fill_parent"
-              android:orientation="vertical" >
+              android:layout_height="fill_parent" android:orientation="vertical">
     <TextView android:id="@+id/text"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
-              android:text="" />
-    <Button android:id="@+id/button"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="Modere seu linguajar!" />
+              android:text="Texto aqui" android:layout_gravity="center_horizontal"/>
+    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="Modere seu linguajar!" android:layout_gravity="center_horizontal"></Button>
 </LinearLayout>

res/xml/citadroid_widget_info.xml

+<?xml version="1.0" encoding="utf-8"?>
+<appwidget-provider
+  xmlns:android="http://schemas.android.com/apk/res/android"
+    android:minWidth="294dp"
+    android:minHeight="72dp"
+    android:initialLayout="@layout/citadroid_widget"
+    android:updatePeriodMillis="5000">
+</appwidget-provider>

src/com/seaandsailor/citadroid/CitaDroid.java

 package com.seaandsailor.citadroid;
 
-import java.io.BufferedReader;
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.Random;
-
 import com.seaandsailor.citadroid.R;
 
 import android.app.Activity;
 	/** Called when the activity is first created. */
 	private Button generate;
 	private TextView text;
-	private ArrayList<String> quotes;
-	private Random rnd;
+	private QuoteDb quotes;
 
 	@Override
 	public void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.main);
-		quotes = new ArrayList<String>();
-		rnd = new Random(System.currentTimeMillis());
 		InputStream txt = getResources().openRawResource(R.raw.batema);
-		try {
-			BufferedReader input = new BufferedReader(
-					new InputStreamReader(txt));
-			try {
-				String line = null;
-				while ((line = input.readLine()) != null) {
-					quotes.add(line);
-				}
-			} finally {
-				input.close();
-			}
-		} catch (IOException ex) {
-			ex.printStackTrace();
-		}
+		quotes = new QuoteDb(txt);
 		generate = (Button) this.findViewById(R.id.button);
 		text = (TextView) this.findViewById(R.id.text);
+		text.setText(quotes.getQuote());
 		this.generate.setOnClickListener(new OnClickListener() {
 			public void onClick(View v) {
-				text.setText(quotes.get(rnd.nextInt(quotes.size())));
+				text.setText(quotes.getQuote());
 			}
 		});
 	}

src/com/seaandsailor/citadroid/CitaDroidWidgetProvider.java

+/*
+ * Baseado no exemplo SimpleWiktionary, que � disponibilizado com a SDK sob a licen�a Apache.
+ *  
+ */
+
+package com.seaandsailor.citadroid;
+
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.os.IBinder;
+import android.widget.RemoteViews;
+
+public class CitaDroidWidgetProvider extends AppWidgetProvider {
+	
+	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
+		// To prevent any ANR timeouts, we perform the update in a service
+        context.startService(new Intent(context, UpdateService.class));
+    }
+	
+	public static class UpdateService extends Service {
+        @Override
+        public void onStart(Intent intent, int startId) {
+            // Build the widget update for now
+            RemoteViews updateViews = buildUpdate(this);
+
+            // Push update for this widget to the home screen
+            ComponentName thisWidget = new ComponentName(this, CitaDroidWidgetProvider.class);
+            AppWidgetManager manager = AppWidgetManager.getInstance(this);
+            manager.updateAppWidget(thisWidget, updateViews);
+        }
+
+        /**
+         * Build a widget update to show a new quotation
+         */
+        public RemoteViews buildUpdate(Context context) {
+            // Pick out month names from resources
+            Resources res = context.getResources();
+            QuoteDb quotes = new QuoteDb(res.openRawResource(R.raw.batema));
+            // Build an update that holds the updated widget contents
+            RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.citadroid_widget);
+            updateViews.setTextViewText(R.id.linktoapp, quotes.getQuote());
+                
+                // When user clicks on widget, do nothing
+               /* String definePage = res.getString(R.string.template_define_url,
+                        Uri.encode(wordTitle));
+                Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));
+                PendingIntent pendingIntent = PendingIntent.getActivity(context,
+                        0  no requestCode , defineIntent, 0  no flags );
+                updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);*/
+            return updateViews;
+        }
+
+        @Override
+        public IBinder onBind(Intent intent) {
+            // We don't need to bind to this service
+            return null;
+        }
+    }
+	
+}

src/com/seaandsailor/citadroid/QuoteDb.java

+package com.seaandsailor.citadroid;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Random;
+
+public class QuoteDb {
+	private ArrayList<String> quotes;
+	private Random rnd;
+
+	public QuoteDb(InputStream txt) {
+		quotes = new ArrayList<String>();
+		rnd = new Random(System.currentTimeMillis());
+		try {
+			BufferedReader input = new BufferedReader(
+					new InputStreamReader(txt));
+			try {
+				String line = null;
+				while ((line = input.readLine()) != null) {
+					quotes.add(line);
+				}
+			} finally {
+				input.close();
+			}
+		} catch (IOException ex) {
+			ex.printStackTrace();
+		}
+	}
+	
+	public String getQuote() {
+		return quotes.get(rnd.nextInt(quotes.size()));
+	}
+	
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.