Android:给应用界面提供快捷方式

Android:给应用界面提供快捷方式


前言

        在平时使用微信,支付宝等APP的时候,我们长按应用图标,其会弹出扫一扫等应用界面快捷方式,如图所示。

        最近观看动脑学院的安卓教程,里面正好有这一功能的实现教程。但按照里面敲出来会发生许多报错,在这里将代码和我遇到的错误以及解决方法po出来供大家借鉴。


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

一、代码

        首先,在res文件夹中新建一个目录“xml”,新建一个xml文件叫做shortcuts.xml,如下图所示。

         其代码如下:

<?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>

         需要注意的是,shortcutShortLabel和shortcutLongLabel不能直接在双引号内写对应的内容,而应该在values目录下的string.xml配置相关内容,然后回到该文件引用。

        string.xml代码如下:

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

        然后在AndroidMainifest.xml中进行配置,我以主页面作为启动页面,在其中加入<meta-data>标签,如下图所示。

        这样的话流程就走完了。 

 

二、遇到的问题和解决方法

1.不知道包名

        在视频中,老师在AndroidManifest.xml找到包名,但是我点开我的文件,里面没有显示包名,我找了好久也不知道对应的包名:

         于是在shortcuts.xml中寻找targetPackage不知道该写啥,所以需要找一种方式得到包名。

        我通过下面方法获得了包名:

        修改MainActivity.java文件:

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);
    }
}

         主页面的layout代码如下:

<?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>

        运行之后,得到包名,如图所示:

         于是将 包名加入配置文件、以及targetPackage便解决该问题。

2.运行时显示错误

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

        刚开始显示这个错误,经过我一番细致检查,我发现是打错字了……shortcutId的最后一个字母打成了大写……

        不过修改完之后还是报错,我查阅了资料:这个错误通常是由于在 Android 版本较早的设备上使用了android:shortcutId,而它只在 Android 7.1(API 级别 25)及以上版本中可用,所以需要解决该错误。

        于是我在我项目的build.gradle文件查找我的minSdk版本,发现刚好24……于是进行修改

        修改后右上角会弹出信息,点击Sync Now,不要点击ingnore these changes。

        修改之后就可以成功运行了。

三、效果

        如图所示:


猜你喜欢

转载自blog.csdn.net/crabxd/article/details/130531759