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();
}