Create a custom Intent Action
and set it as PendingIntent
to the widget
item ( button in your case)
Intent intent = new Intent("com.example.app.ACTION_PLAY");
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.bPlay, pendingIntent);
Then, change your manifest to handle the Action
passed in PendingIntent
<service android:name="com.example.app.services.CustomService" >
<intent-filter>
<action android:name="com.mediabook.app.ACTION_PLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
Finally , when the play button is clicked , the Service
will receive the Action
and started. Check for the Action
in onStartCommand
@Override
public int onStartCommand(Intent intent, int flag, int startId) {
if(intent.getAction().equalsIgnoreCase("com.example.app.ACTION_PLAY")){
// do your stuff here
}
You can set similar custom Actions
for all the required views in the Widget
.
See the compile code here https://gist.github.com/androidbensin/4a9f044ac0b3110c049e
Hope your are good now. Let me know if any issues