android自定义桌面应用

android自定义桌面应用

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

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

主要的代码如下:

1.manifest声明

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

3: 创建item布局



    
    

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>

运行效果如下:

android自定义桌面应用_第1张图片

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

android自定义桌面应用_第2张图片

你可能感兴趣的:(android,android)