Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Created November 21, 2013 11:05
Show Gist options
  • Save ishitcno1/7579790 to your computer and use it in GitHub Desktop.
Save ishitcno1/7579790 to your computer and use it in GitHub Desktop.
Implement android widget
<receiver android:name=".MyAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
<?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">
<ImageView
android:id="@+id/iv_appwidget"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_gravity="center"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:paddingTop="7dp"
android:gravity="center"
android:text="@string/widget_label"
android:textColor="@android:color/white"
android:shadowColor="@android:color/black"
android:shadowRadius="2"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="48dp"
android:minHeight="48dp"
android:updatePeriodMillis="0"
android:previewImage="@drawable/ic_launcher"
android:initialLayout="@layout/appwidget"
android:widgetCategory="home_screen"
android:resizeMode="horizontal|vertical">
</appwidget-provider>
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent intent = new Intent(context, AIntentService.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setOnClickPendingIntent(R.id.iv_appwidget, pi);
appWidgetManager.updateAppWidget(appWidgetIds[0], views);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment