ToB企服应用市场:ToB评测及商务社交产业平台

标题: Android学习21 -- launcher [打印本页]

作者: 王海鱼    时间: 2025-2-14 23:38
标题: Android学习21 -- launcher
1 前言

之前在工作中,第一次听到launcher有点蒙圈,不知道是啥,当时还赶鸭子上架去和客户PK launcher的事。厥后才知道其实就是安卓的桌面。本来还以为很复杂,毕竟之前接触过windows的桌面,那叫一个复杂。。。

后面查了一下,Android的桌面倒是没那么复杂,本质其实就是一个App,系统启动之后跑起来的第一个app。
   Android Launcher 是一个应用步调,用于表现设备的主屏幕,并且是启动其他应用的入口。简单来说,Launcher 是 Android 设备的“应用启动器”,负责管理和展示设备的主屏幕、应用图标、桌面小部件(widgets)以及应用步调的启动举动。
  
2 利用步骤

最近搭建了app的情况,现在借着之前搭建的情况(Android学习20 -- 手搓App2(Gradle)-CSDN博客),试着弄一个launcher。
   *   The key to making your app a launcher is to declare the correct intent filter in your `AndroidManifest.xml` file. This tells the Android system that your app is capable of acting as a launcher.
*   Here's how your `AndroidManifest.xml` should look (specifically the `<activity>` tag for your
  所以修改之后的AndroidManifest.xml是如许。
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.     package="com.example.simpleapp">
  3.     <application
  4.         android:allowBackup="true"
  5.         android:icon="@mipmap/ic_launcher"
  6.         android:label="SimpleApp"
  7.         android:theme="@style/Theme.AppCompat.DayNight">
  8.         
  9.         <activity
  10.             android:name=".MainActivity"
  11.             android:label="SimpleApp">
  12.             
  13.             <intent-filter>
  14.                 <action android:name="android.intent.action.MAIN" />
  15.                 <category android:name="android.intent.category.LAUNCHER" />
  16.                     <category android:name="android.intent.category.HOME" />
  17.                     <category android:name="android.intent.category.DEFAULT" />
  18.             </intent-filter>
  19.             
  20.         </activity>
  21.     </application>
  22. </manifest>
复制代码
最核心的就是在intent filter内里的配置,重点就是这几行。
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
    * **Explanation:** *
  `<action android:name="android.intent.action.MAIN" />`: This is the standard action for the main entry point of an app. * `
  <category android:name="android.intent.category.LAUNCHER" />`: This category indicates that this activity should be listed in the app launcher (the list of apps). * `
  <category android:name="android.intent.category.HOME" />`: This is the crucial category that tells the system this activity can act as a home screen replacement. * `
  <category android:name="android.intent.category.DEFAULT" />`: This category is often used with `HOME` to ensure your app is considered a valid home screen option. * 
  
MainActivity.java。主界面上面就是一个按钮,点了之后会启动一个浏览器。
  1. package com.example.simpleapp;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity {
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         // 获取按钮控件
  14.         Button openBrowserButton = findViewById(R.id.openBrowserButton);
  15.         
  16.         // 设置按钮点击事件
  17.         openBrowserButton.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 // 启动浏览器
  21.                 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
  22.                 startActivity(browserIntent);
  23.             }
  24.         });
  25.     }
  26. }
复制代码
这里就实现了前面说的启动应用步调的功能,就是通过startActivity(browserIntent);

之后要指定布局,也就是res/layout/activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     android:gravity="center">
  7.     <!-- 启动浏览器按钮 -->
  8.     <Button
  9.         android:id="@+id/openBrowserButton"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="Open Browser" />
  13. </LinearLayout>
复制代码

编译用之前调好的就行。
  1. gradle wrapper
  2. gradlew assembleDebug
复制代码
编译的时候会报错,说mipmap,drawable找不到。

可以去Android Studio创建好的工程内里拷贝过来。完了大概是如许的。内里也就是一堆xml。

之后就可以顺遂编过,安装也比较简单。直接运行就是如许:


3 未能终极完成。。

但是有一点很遗憾,在荣耀的平板内里,这个是叫做桌面。自己做的这个launcher,怎么都没法设置进去。除了默认的华为桌面,找不到可以设置的地方。


查了一下,有可能是我的设置还有点问题,但是大概率还是华为的平板把这部分的权限给关了。估计只有以后找一个干净的AOSP再试试了。。。 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4