Skip to content

Commit 348c05f

Browse files
committed
now twitter widgets with onClickListener
2 parents 9b5b641 + 20f7a39 commit 348c05f

23 files changed

Lines changed: 509 additions & 9 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>de.vogella.android.locationapi.maps</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

‎de.vogella.android.widget.example/AndroidManifest.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<meta-data android:name="android.appwidget.provider"
1212
android:resource="@xml/widget_info" />
1313
</receiver>
14+
<service android:name=".UpdateWidgetService"></service>
15+
<activity android:name=".Test"></activity>
1416
</application>
1517
<uses-sdk android:minSdkVersion="10" />
1618

‎de.vogella.android.widget.example/res/layout/widget_layout.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="match_parent" android:layout_height="match_parent"
3+
android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent"
44
android:background="@drawable/myshape" android:layout_margin="8dip">
55
<TextView android:layout_margin="4dip" android:text="Static Text"
66
android:id="@+id/TextView01" android:layout_width="match_parent"
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
package de.vogella.android.widget.example;
22

3-
import java.util.Random;
4-
3+
import android.app.PendingIntent;
54
import android.appwidget.AppWidgetManager;
65
import android.appwidget.AppWidgetProvider;
76
import android.content.Context;
7+
import android.content.Intent;
88
import android.widget.RemoteViews;
99

1010
public class MyWidgetProvider extends AppWidgetProvider {
1111
@Override
1212
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
1313
int[] appWidgetIds) {
14-
// Get some useful info from the web
15-
String fakeUpdate = "Random: ";
16-
Random random = new Random();
17-
int nextInt = random.nextInt(100);
18-
fakeUpdate += String.valueOf(nextInt);
14+
15+
// Build the intent to call the service
1916
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
2017
R.layout.widget_layout);
21-
remoteViews.setTextViewText(R.id.TextView01, fakeUpdate);
18+
Intent intent = new Intent(context.getApplicationContext(),
19+
UpdateWidgetService.class);
20+
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
21+
22+
// To react to a click we have to use a pending intent as the
23+
// onClickListener is
24+
// excecuted by the homescreen application
25+
PendingIntent pendingIntent = PendingIntent.getService(
26+
context.getApplicationContext(), 0, intent,
27+
PendingIntent.FLAG_UPDATE_CURRENT);
28+
remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
29+
30+
// Finally update all widgets with the information about the click
31+
// listener
2232
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
33+
34+
// Update the widgets via the service
35+
context.startService(intent);
2336
}
2437
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package de.vogella.android.widget.example;
2+
3+
import java.util.Random;
4+
5+
import android.app.Service;
6+
import android.appwidget.AppWidgetManager;
7+
import android.content.Intent;
8+
import android.os.IBinder;
9+
import android.util.Log;
10+
import android.widget.RemoteViews;
11+
12+
public class UpdateWidgetService extends Service {
13+
@Override
14+
public void onStart(Intent intent, int startId) {
15+
Log.i("UpdateWidgetService", "Called");
16+
// Create some random data
17+
String fakeUpdate = null;
18+
Random random = new Random();
19+
20+
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
21+
.getApplicationContext());
22+
23+
int[] appWidgetIds = intent
24+
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
25+
if (appWidgetIds.length > 0) {
26+
for (int widgetId : appWidgetIds) {
27+
int nextInt = random.nextInt(100);
28+
fakeUpdate = "Random: " + String.valueOf(nextInt);
29+
RemoteViews remoteViews = new RemoteViews(getPackageName(),
30+
R.layout.widget_layout);
31+
remoteViews.setTextViewText(R.id.TextView01, fakeUpdate);
32+
appWidgetManager.updateAppWidget(widgetId, remoteViews);
33+
}
34+
stopSelf();
35+
}
36+
super.onStart(intent, startId);
37+
}
38+
39+
@Override
40+
public IBinder onBind(Intent intent) {
41+
return null;
42+
}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="gen"/>
5+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>de.vogella.android.widget.followers</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.jdt.core.javabuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
31+
<nature>org.eclipse.jdt.core.javanature</nature>
32+
</natures>
33+
</projectDescription>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="de.vogella.android.widget.followers" android:versionCode="1"
4+
android:versionName="1.0">
5+
<uses-sdk android:minSdkVersion="7" />
6+
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
7+
8+
<application android:icon="@drawable/icon" android:label="@string/app_name">
9+
<receiver android:name="TwitterFollowerReceiver">
10+
<intent-filter>
11+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
12+
</intent-filter>
13+
<meta-data android:name="android.appwidget.provider"
14+
android:resource="@xml/widget_info" />
15+
</receiver>
16+
<activity android:name="TwitterFollowerConfigActivity">
17+
<intent-filter>
18+
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
19+
</intent-filter>
20+
</activity>
21+
<service android:name="TwitterFollowerService"></service>
22+
23+
</application>
24+
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system use,
7+
# "build.properties", and override values to adapt the script to your
8+
# project structure.
9+
10+
# Project target.
11+
target=android-7
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-optimizationpasses 5
2+
-dontusemixedcaseclassnames
3+
-dontskipnonpubliclibraryclasses
4+
-dontpreverify
5+
-verbose
6+
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7+
8+
-keep public class * extends android.app.Activity
9+
-keep public class * extends android.app.Application
10+
-keep public class * extends android.app.Service
11+
-keep public class * extends android.content.BroadcastReceiver
12+
-keep public class * extends android.content.ContentProvider
13+
-keep public class * extends android.app.backup.BackupAgentHelper
14+
-keep public class * extends android.preference.Preference
15+
-keep public class com.android.vending.licensing.ILicensingService
16+
17+
-keepclasseswithmembernames class * {
18+
native <methods>;
19+
}
20+
21+
-keepclasseswithmembernames class * {
22+
public <init>(android.content.Context, android.util.AttributeSet);
23+
}
24+
25+
-keepclasseswithmembernames class * {
26+
public <init>(android.content.Context, android.util.AttributeSet, int);
27+
}
28+
29+
-keepclassmembers enum * {
30+
public static **[] values();
31+
public static ** valueOf(java.lang.String);
32+
}
33+
34+
-keep class * implements android.os.Parcelable {
35+
public static final android.os.Parcelable$Creator *;
36+
}

0 commit comments

Comments
 (0)