-
-
Save RowlandOti/25142a10fbfc7833b54fd7ed6825abfc to your computer and use it in GitHub Desktop.
Android App Widget sample app using setOnClickPendingIntent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.test1" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="17" | |
android:targetSdkVersion="19" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<receiver android:name=".ExampleAppWidgetProvider"> | |
<meta-data android:name="android.appwidget.provider" | |
android:resource="@xml/appwidget_provider" /> | |
<intent-filter> | |
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> | |
</intent-filter> | |
</receiver> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" | |
android:minWidth="60dp" | |
android:minHeight="30dp" | |
android:updatePeriodMillis="86400000" | |
android:initialLayout="@layout/appwidget_provider_layout" | |
android:resizeMode="horizontal" | |
> | |
</appwidget-provider> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<ImageButton | |
android:id="@+id/widget_play" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:scaleType="fitCenter" | |
android:adjustViewBounds="true" | |
android:layout_marginRight="5dp" | |
android:src="@drawable/run"/> | |
<ImageButton | |
android:id="@+id/widget_pause" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:scaleType="fitCenter" | |
android:adjustViewBounds="true" | |
android:layout_marginRight="5dp" | |
android:src="@drawable/pause"/> | |
<ImageButton | |
android:id="@+id/widget_reset" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:scaleType="fitCenter" | |
android:adjustViewBounds="true" | |
android:src="@drawable/reset"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleAppWidgetProvider extends AppWidgetProvider { | |
private static final String TAG = "ExampleAppWidgetProvider"; | |
private String playAction = "playService"; | |
private String stopAction = "stopService"; | |
private String resetAction = "resetService"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
Log.v(TAG, "Action received = " + action); | |
super.onReceive(context, intent); | |
} | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); | |
views.setOnClickPendingIntent(R.id.widget_play, this.getPendingSelfIntent(context, playAction)); | |
views.setOnClickPendingIntent(R.id.widget_pause, this.getPendingSelfIntent(context, stopAction)); | |
views.setOnClickPendingIntent(R.id.widget_reset, this.getPendingSelfIntent(context, resetAction)); | |
appWidgetManager.updateAppWidget(new ComponentName(context, ExampleAppWidgetProvider.class), views); | |
} | |
protected PendingIntent getPendingSelfIntent(Context context, String action) { | |
Intent intent = new Intent(context, ExampleAppWidgetProvider.class); | |
intent.setAction(action); | |
return PendingIntent.getBroadcast(context, 0, intent, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment