Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O Pinned Shortcuts - CREATE_SHORTCUT intent filter

In the documentation for the new "Pinned Shortcuts" feature in Android O, they mention that "You can also create a specialized activity that helps users create shortcuts, complete with custom options and a confirmation button".

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

Here's the declaration in the Manifest:

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

P.S
In the documentation they also shows an example in the Gmail app - how do I get to that screen? I wanted to see the flow but I couldn't find it.

like image 321
Ori Wasserman Avatar asked Oct 10 '17 08:10

Ori Wasserman


2 Answers

You have to create a new activity as dialog, then you can add whatever options you want on that dialog and handle as you want the addShortcut method.

I tried that code to add and remove a dynamical 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>
like image 119
gmetax Avatar answered Sep 27 '22 19:09

gmetax


In you 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);
like image 43
Giuseppe Raddato Avatar answered Sep 27 '22 19:09

Giuseppe Raddato