0

Here i have made one widget and added its click listener where i'm starting one service.It works perfectly.But once user force stops application from appinfo my widget's click is not responding.I m not getting why this is happening.Help on this will be appreciated.Thank you

My app widget class code:

MyWidget.java:

public class MyWidget extends AppWidgetProvider {

    static Context cont;
    static SharedPreferences preferences;


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        cont = context;
        Intent intent2 = new Intent();

        intent2.setAction("....");
        PendingIntent pendingIntent = PendingIntent.
                getBroadcast(context, 0,
                        intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
        views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them

        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }
@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
                context.startService(new Intent(context, MyService.class));

       }
  @Override
    public void onEnabled(Context context) {
..................  }
  @Override
    public void onDisabled(Context context) {
  .................}
user8209191
  • 37
  • 1
  • 7
  • 1
    Switch to using an explicit Intent (`new Intent(context, YourReceiver.class)`) instead of an implicit `Intent` (`setAction("....");`). At minimum, this will eliminate some security issues. It *might* help in this case, as an explicit `Intent` is the way to move apps out of the stopped state. – CommonsWare May 19 '18 at 20:43
  • @CommonsWare - thanks..that helped. – user8209191 May 20 '18 at 12:16

1 Answers1

0

Always use an explicit Intent whenever possible. Your code would not work on Android 8.0 and higher, where implicit broadcasts are banned. And an explicit Intent can move your app out of the stopped state.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491