Android widget does not start service

I need to make a widget that after clicking will start the service. The service will perform a certain action, change the image of widgets, play sound, etc. And it will stop.

AppWidgetProvider:

public class WidgetActivity extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); Intent intent = new Intent(context.getApplicationContext(), SilentService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); PendingIntent pendingIntent = PendingIntent.getService( context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.LinLayWiget, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); context.startService(intent); } } 

service:

 public class SilentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "I'm Alive!", 1500).show(); MediaPlayer mpl = MediaPlayer.create(this, R.raw.enter ); mpl.start(); } @Override public void onDestroy() { //code to execute when the service is shutting down } @Override public void onStart(Intent intent, int startid) { //code to execute when the service is starting up } } 

manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="FX.Widget" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="widget.cam.com.WidgetActivity" android:label="FXMaster" android:icon="@drawable/assiconwi"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="android.appwidget.action.APPWIDGET_ENABLED"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widgetprovider" /> </receiver> <service android:enabled="true" android:name=".SilentService"> </service> </application> </manifest> 

But as soon as I click on the widget nothing happens ... any ideas why?

Thanks!

+4
source share
1 answer

Check out this tutorial. As soon as you can get an idea.

Widget tutorial using the service

+2
source

Source: https://habr.com/ru/post/1395929/


All Articles