使用以下代码无法正常工作。我试图在运行时启用小部件,但这不会发生
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" />
在此处主要活动中启用接收器的方法我想通过使用接收器启用小部件。
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();
}