Android: Provide shortcuts to application interfaces

Android: Provide shortcuts to application interfaces


Preface

        When we usually use WeChat, Alipay and other apps, we long press the application icon, and it will pop up shortcuts to the application interface such as scanning, as shown in the figure.

        ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​: ​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​However, many errors will occur when typing out the code. Here I will post the code, the errors I encountered, and the solutions for everyone to learn from.


 

提示:以下是本篇文章正文内容,下面案例可供参考

1. Code

        First, create a new directory "xml" in the res folder, and create a new xml file called shortcuts.xml, as shown in the figure below.

         The code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/first_short"
        android:shortcutLongLabel="@string/first_long">

       <intent
           android:action="android.intent.action.VIEW"
           android:targetPackage="com.example.hotel"
           android:targetClass="com.example.hotel.ui.MainActivity"/>
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

</shortcuts>

         It should be noted that shortcutShortLabel and shortcutLongLabel cannot directly write the corresponding content within double quotes. Instead, the relevant content should be configured in string.xml in the values ​​directory, and then returned to the file for reference.

        The string.xml code is as follows:

<resources>
    <string name="app_name">crab</string>
    <string name="first_short">first</string>
    <string name="first_long">首页</string>
</resources>

        Then configure it in AndroidMainifest.xml. I use the main page as the startup page and add the <meta-data> tag, as shown in the figure below.

        In this case, the process is complete.​ 

 

2. Problems encountered and solutions

1. Don’t know the package name

        In the video, the teacher found the package name in AndroidManifest.xml, but when I clicked on my file, the package name was not displayed. I searched for a long time and still didn’t know the corresponding package name:

         So I looked for targetPackage in shortcuts.xml and didn’t know what to write, so I needed to find a way to get the package name.

        ​ ​ ​I obtained the package name through the following method:

        Modify the MainActivity.java file:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String packageName = getPackageName();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(packageName);
        TextView tv_receive = findViewById(R.id.tv_receive);
        String desc = String.format("包名为:%s",packageName);
        tv_receive.setText(desc);
    }
}

         The layout code of the main page is as follows:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是主页面!!!还未开发完成!!!!!!!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

        After running, you will get the package name, as shown in the figure:

         So adding the package name to the configuration file and targetPackage will solve the problem.

2. Display error when running

ERROR:D:\AndroidProject\xxx\app\src\main\res\xml\shortcuts.xml:7: AAPT: error: attribute android:shortcutID not found.

        This error was first displayed. After careful inspection, I found that it was a typo...the last letter of shortcutId was capitalized...

        However, after modifying it, an error still occurred. I checked the information: This error is usually caused by using android:shortcutId on devices with earlier Android versions, and it is only available in Android 7.1 (API Available in version 25) and above, so this bug needs to be resolved.

        So I looked for my minSdk version in the build.gradle file of my project and found that it was exactly 24...so I modified it.

        After modification, a message will pop up in the upper right corner, click Sync Now, do not click ignore these changes.

        After modification, it can run successfully.

3. Effect

        as the picture shows:


Guess you like

Origin blog.csdn.net/crabxd/article/details/130531759