0

It is not working by using the below code. I tried to enable the widget at runtime but this is not happening

In Manifest file Receiver is declaring like that to register a receiver in as defaulted to enable false.

<receiver android:enabled="false" android:name="com.my.app.MyWidget">
            <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED"/>
              <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" />
 </receiver>


 <service android:name="com.my.app.MYWidetService" />

method to enable the receiver in the main activity here i want to enable the widget by using the receiver.

public void enableHomeScreenWidget(){
        try {
            ComponentName receiver = new ComponentName(getBaseContext(), MyWidget.class);
            PackageManager pm = this.getPackageManager();
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

        }catch (Exception e){
            e.printStackTrace();
        }
      }

  calling the enableHomeScreenWidget() in side the onReume() like below.

       @Override
        protected void onResume() {
            super.onResume();
            enableHomeScreenWidget();
        }
  • i tried to call widget enabling method in onCreate() of the activity as well but no use. – Subramanyam M Mar 29 '18 at 10:32
  • Possible duplicate of [How to enable and disable a component?](https://stackoverflow.com/questions/17409907/how-to-enable-and-disable-a-component) – HeyAlex Mar 29 '18 at 14:25

0 Answers0