Created
May 9, 2014 01:53
-
-
Save libinbensin/4a9f044ac0b3110c049e to your computer and use it in GitHub Desktop.
Widget with Service (Custom Action)
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
/** 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> |
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 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; | |
} | |
} |
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
// 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); | |
} | |
} |
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
<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