Skip to content

Instantly share code, notes, and snippets.

@libinbensin
Created May 9, 2014 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save libinbensin/4a9f044ac0b3110c049e to your computer and use it in GitHub Desktop.
Save libinbensin/4a9f044ac0b3110c049e to your computer and use it in GitHub Desktop.
Widget with Service (Custom Action)
/** change the service in manifest to receive custom action **/
<service android:name="com.stackwork.app.services.CustomService" >
<intent-filter>
<action android:name="com.mediabook.app.ACTION_PLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
public class CustomService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flag, int startId) {
if(intent != null && intent.getAction().equalsIgnoreCase("com.example.app.ACTION_PLAY")){
// do your stuff here
}
return START_STICKY;
}
}
// create a custom widget provider
public class CustomWidgetProvider extends AppWidgetProvider{
public static final String ACTION_PLAY = "com.example.app.ACTION_PLAY";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
Intent intent = new Intent(ACTION_PLAY);
PendingIntent pendingIntent = PendingIntent.getService(context, 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.play, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_margin="10dp"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_margin="10dp"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_margin="10dp"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment