Android O Pinned Shortcuts - intent filter CREATE_SHORTCUT

In the documentation for the new “Pinned Shortcuts” feature in Android O, they note that “you can also create custom activities that help users create shortcuts, complete with user options and a confirmation button.

I tried to execute the documentation, but when I tried to create a new shortcut, I saw only the default dialog, not my activity.

Here is the declaration in the manifest:

<activity android:name=".ShortcutActivity"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity> 

PS
In the documentation, they also show an example in the Gmail application - how do I get to this screen? I wanted to see the stream, but I could not find it.

+5
source share
4 answers

You need to create a new action dialog, then you can add any parameters you want in this dialog and process the addShortcut method as you want.

I tried this code to add and remove a dynamic shortcut and it worked.

AndroidManifest.xml

 <activity android:name=".DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog" > ... </activity> 

DialogActivity.java

 public class DialogActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); this.setFinishOnTouchOutside(false); findViewById(R.id.add_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addShortcut(); } }); findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } ... } 

activity_dialog.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_dialog" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.metax.myapplication.DialogActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Do you want to add shortcut?"/> <Button android:id="@+id/cancel_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_alignParentStart="true" android:text="No thanks"/> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_alignParentEnd="true" android:text="Add me"/> </RelativeLayout> 
+3
source

In your java class insert

  ShortcutManager sM = c.getSystemService(ShortcutManager.class); Intent intent2 = new Intent(c.getApplicationContext(), ShortcutActivity.class); intent2.setAction(Intent.ACTION_VIEW); ShortcutInfo shortcut2 = new ShortcutInfo.Builder(c,MSG_SHORCUT_CUSTOM) .setIntent(intent2) .setShortLabel("ShortLabel") .setLongLabel("LongLaber") .setDisabledMessage("DisabledMessage") .setIcon(Icon.createWithResource(c, R.mipmap.ic_add_outline_short)) .build(); listshortcut.add(shortcut2); Intent pinnedShortcutCallbackIntent = mShortcutManager.createShortcutResultIntent(shortcut2); PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, 0); mShortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender()); sM.setDynamicShortcuts(listshortcut); 
0
source

To start a shortcut action with the ACTION_CREATE_SHORTCUT action, click on the application icon and click on the widget icon, you will see a 1x1 widget, when clicked, your desired activity will be launched.

You can also explicitly dismiss this intention from your application, as well as about any desired action.

0
source

Create a new resource file: res / xml / shortcuts.xml. This is how you create shortcuts after you made a nicicary manifest change

 <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="compose" android:enabled="true" android:icon="@drawable/compose_icon" android:shortcutShortLabel="@string/compose_shortcut_short_label1" android:shortcutLongLabel="@string/compose_shortcut_long_label1" android:shortcutDisabledMessage="@string/compose_disabled_message1"> <intent android:action="android.intent.action.VIEW" android:targetPackage="your package" android:targetClass="com.example.myapplication.ComposeActivity" /> </shortcut> <!-- Specify more shortcuts here. --> </shortcuts> 

In the manifest, add this to the activity tag,

 <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> 

If you want to read more, check out the doc , which explains about attached shortcuts, static and dynamic shortcuts.

Here is an example from google, in my repo samples

-2
source

Source: https://habr.com/ru/post/1272488/


All Articles