Android music player widget

后端 未结 2 679
天命终不由人
天命终不由人 2020-12-30 13:14

I\'m working on a music player-widget (home screen widget).. It only needs to play 1 song, (Preferably with the MediaPlayer class). But I\'m not sure how to implement it. I\

2条回答
  •  醉酒成梦
    2020-12-30 13:49

    In the onUpdate(...) method of your AppWidgetProvider, use something like this to start a service (this example associates the service to a button click event):

    Intent intent = new Intent(context, MusicService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    
    // Get the layout for the App Widget and attach an on-click listener to the button
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
    views.setOnClickPendingIntent(R.id.button, pendingIntent);
    

    For more info look here

提交回复
热议问题