'Where do widgets declare themselves?
I downloaded project "Contacts" from Google's git: https://android.googlesource.com/platform/packages/apps/Contacts/+/refs/heads/master
to look at how widgets were there implemented, but I can't find declaration of them in AndroidManifest.xml
, as it should be according to wiki:
I also can't find .xml file that should contain <appwidget-provider
, according to wiki:
But widgets in the app are present:
So how Google inits widgets now?
Solution 1:[1]
Found it. They appear as widgets but in reality they are activity-alias
es with
android.intent.action.CREATE_SHORTCUT
intent-filter:
<activity-alias
android:name="ContactShortcut"
android:icon="@drawable/logo_quick_contacts_color_44in48dp"
android:label="@string/shortcutContact"
android:exported="true"
android:targetActivity=".activities.ContactSelectionActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity-alias>
<activity-alias
android:name="alias.DialShortcut"
android:icon="@drawable/logo_quick_contacts_dialer_color_44in48dp"
android:label="@string/shortcutDialContact"
android:exported="true"
android:targetActivity=".activities.ContactSelectionActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.CAR_MODE"/>
</intent-filter>
</activity-alias>
<activity-alias
android:name="alias.MessageShortcut"
android:icon="@drawable/logo_quick_contacts_mail_color_44in48dp"
android:label="@string/shortcutMessageContact"
android:exported="true"
android:targetActivity=".activities.ContactSelectionActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity-alias>
In the end all these activities call setResult()
with an intent that contains in its extras name, icon and intent of future shortcut:
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
setResult(RESULT_OK, intent);
finish();
And then Launcher receives it in onActivityResult
and creates an app-shortcut (not widget) on desktop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |