android自定义桌面应用

android自定义桌面应用

这篇文章主要讲下自定义桌面应用.

效果主要是调用packageManager来获取当前所有的程序,并在自定义桌面程序中展示,并支持跳转.

主要的代码如下:

1.manifest声明

“android.intent.category.HOME” 是Android中的一个Intent过滤器类别,用于指定一个Activity作为设备的主屏幕。
当用户按下设备的Home键时,系统会启动具有该过滤器的Activity,并将其设置为设备的主屏幕。

   <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

2: 创建adapter

package com.test.luanchertest;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * @Author: zh
 * @Time: 23-11-27.
 * @Email:
 * @Describe:
 */
public class LauncherAdapter extends BaseAdapter {
    private List<ResolveInfo> mApps;
    private PackageManager mPackageManager;
    private Context context;

    public LauncherAdapter(Context context) {
        this.context = context;
        mPackageManager = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mApps = mPackageManager.queryIntentActivities(mainIntent, 0);
    }

    @Override
    public int getCount() {
        return mApps.size();
    }

    @Override
    public Object getItem(int i) {
        return mApps.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_launcher, viewGroup, false);
        }
        ResolveInfo info = mApps.get(i);
        ImageView icon = (ImageView) view.findViewById(R.id.item_icon);
        TextView label = (TextView) view.findViewById(R.id.item_label);
        icon.setImageDrawable(info.loadIcon(mPackageManager));
        label.setText(info.loadLabel(mPackageManager));
        String packageName = info.activityInfo.packageName;
        Log.e("xxxxxxx", "getView: "+packageName );
        Intent launchIntent = mPackageManager.getLaunchIntentForPackage(packageName);
        icon.setOnClickListener(v -> context.startActivity(launchIntent));
        label.setOnClickListener(v -> context.startActivity(launchIntent));
        return view;
    }
}

ResolveInfo类的主要属性包括:

activityInfo:表示应用程序组件的Activity信息,包括包名、类名等。
serviceInfo:表示应用程序组件的Service信息,包括包名、类名等。
providerInfo:表示应用程序组件的Content Provider信息,包括包名、类名等。
filter:表示应用程序组件的Intent过滤器信息,包括Action、Category、Data等。
icon:表示应用程序组件的图标。
labelRes:表示应用程序组件的标签资源ID。
packageName:表示应用程序组件所属的包名。

通过getLaunchIntentForPackage(),可以获取到启动指定应用程序的Intent对象,
然后可以使用该Intent对象进行应用程序的启动操作.

3: 创建item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="centerCrop"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        />
    <TextView
        android:id="@+id/item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textColor="#000000"
        android:text="xxxx"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

4: mainAcitivty处理

public class MainActivity extends AppCompatActivity {
    private GridView gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView =findViewById(R.id.launcher_grid);
        gridView.setAdapter(new LauncherAdapter(this));
    }
}

主布局:

<?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=".MainActivity">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/launcher_grid"
        android:numColumns="3"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

运行效果如下:

在这里插入图片描述

在设置中可以查找默认桌面应用:

在这里插入图片描述

  • 34
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android仿iOS桌面应用删除动画可以使用属性动画实现。以下是一个简单的实现步骤: 1. 创建一个布局文件,用于显示要删除的应用图标。在这个布局中,你可以使用ImageView来显示应用图标,然后添加一个TextView来显示删除提示。 2. 在Java代码中,使用属性动画设置ImageView的透明度和缩放比例。通过逐渐降低透明度和缩放比例,可以创建一个逐渐消失的效果。 3. 在动画结束时,删除应用程序,并在屏幕上显示一个Snackbar或Toast,以显示已删除的应用程序名称。 下面是一个示例代码,可以让你更好地了解如何实现这个功能: ```java public class DeleteAnimationActivity extends AppCompatActivity { private ImageView mAppIcon; private TextView mDeleteHint; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete_animation); mAppIcon = findViewById(R.id.iv_app_icon); mDeleteHint = findViewById(R.id.tv_delete_hint); // 设置属性动画 ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mAppIcon, "alpha", 1f, 0f); ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mAppIcon, "scaleX", 1f, 0f); ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(mAppIcon, "scaleY", 1f, 0f); // 设置动画集合 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator); animatorSet.setDuration(500); // 监听动画结束事件 animatorSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { // 删除应用程序 deleteApp(); // 显示Snackbar或Toast Snackbar.make(mAppIcon, "已删除应用程序", Snackbar.LENGTH_LONG).show(); } }); // 启动动画 animatorSet.start(); } private void deleteApp() { // 删除应用程序的代码 } } ``` 在上面的代码中,我们使用 ObjectAnimator 来设置透明度和缩放比例的动画,然后使用 AnimatorSet 将它们组合在一起。在动画结束时,我们删除应用程序并显示一个Snackbar或Toast。 如果你想要更加复杂的删除动画效果,可以尝试使用 PathInterpolator 或自定义 Interpolator 来控制动画的速度和加速度。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值