Android安卓-开发一个android桌面

从0开始 开发一个属于自己的桌面程序

最近在开发一个新项目,需要把应用改成桌面,并引导用户设置为默认桌面,完成后的效果如下图:

 

1.添加XML

  <activity android:name=".activitys.DeskTop" android:launchMode="singleTop">
            <intent-filter>
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>

在主Activity里 添加

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

这样用户在点按Home键的时候,就会弹出对话框,提示选择新的默认桌面

2.主Activity布局文件

布局比较简单,线性布局嵌套GridView,下面留了一点空袭是为了腾出空间放置拨号、短信常用APP

<?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="match_parent"
    android:orientation="vertical"
    android:id="@+id/desktop_linear"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2">
        <GridView
            android:id="@+id/mgv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="4"
            ></GridView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="8"></LinearLayout>
</LinearLayout>

3.获取APP列表

获取手机上已经安装的APP列表,同时过滤掉系统应用

public static List<AppInfo> GetAppList1(Context context){
        List<AppInfo> list=new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> activities   = pm.queryIntentActivities(mainIntent, 0);
        for(ResolveInfo info : activities){
            String packName = info.activityInfo.packageName;
            if(packName.equals(context.getPackageName())){
                continue;
            }
            AppInfo mInfo = new AppInfo();
            mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
            mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
            mInfo.setPackageName(packName);
            // 为应用程序的启动Activity 准备Intent
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(packName,
                    info.activityInfo.name));
            mInfo.setIntent(launchIntent);
            list.add(mInfo);
        }
        return list;
    }

AppInfo的类也放出来

public class AppInfo {
    private String packageName; //包名
    private Drawable ico;       //图标
    private String Name;        //应用标签
    private Intent intent;     //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity

    public Intent getIntent() {
        return intent;
    }

    public void setIntent(Intent intent) {
        this.intent = intent;
    }


    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public Drawable getIco() {
        return ico;
    }

    public void setIco(Drawable ico) {
        this.ico = ico;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }
}

4.自定义BaseAdapter填充GridView数据

public class DeskTopGridViewBaseAdapter extends BaseAdapter {

    Context context;
    List<AppInfo> appInfos=new ArrayList<>();

    public DeskTopGridViewBaseAdapter(List<AppInfo> appInfos,Context context){
        this.appInfos=appInfos;
        this.context=context;
    }

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

    @Override
    public Object getItem(int position) {
        return appInfos.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder=null;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.desktop_gridview_item,null);
            holder=new Holder();
            holder.ico=(ImageView) convertView.findViewById(R.id.iv);
            holder.Name=(TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        }else {
            holder= (Holder) convertView.getTag();
        }

        holder.ico.setImageDrawable(appInfos.get(position).getIco());
        holder.Name.setText(appInfos.get(position).getName());

        return convertView;
    }


    static class Holder{
        ImageView ico;
        TextView Name;

    }

 R.layout.desktop_gridview_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="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:layout_margin="5dp"
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="60dp"
        />
    <TextView
        android:layout_margin="5dp"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:text="Lable"/>
</LinearLayout>

 

 

5.在主Activity里填充数据,并给GridView的Item设置点击事件,点击图标之后打开该App

 private void initAppList() {
        appInfos=GetApps.GetAppList1(this);
        DeskTopGridViewBaseAdapter deskTopGridViewBaseAdapter=new DeskTopGridViewBaseAdapter(appInfos,this);
        mGridView.setAdapter(deskTopGridViewBaseAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent=appInfos.get(position).getIntent();
//                startActivity(intent);

                Intent intent = getPackageManager().getLaunchIntentForPackage(appInfos.get(position).getPackageName());
                if (intent != null) {
                    intent.putExtra("type", "110");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

            }
        });
    }

到这里基本就打工搞成了,我们继续设置一下主Activity的背景为当前用户的桌面壁纸

 //设置背景
        WallpaperManager manager =WallpaperManager.getInstance(this);
        Drawable drawable=manager.getDrawable();
        linearLayout.setBackground(drawable);

这里需要注意,需要动态申请这两个权限,注意 是动态申请

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

继续,弹出对话框,让用户设置为默认桌面,这里针对小米、华为、普通安卓都有处理,我直接把代码贴出来

//桌面相关操作

public class Desktop {

    private Context context;

    public Desktop(Context context) {
        this.context = context;
    }

    /**
     * 清除默认桌面(采用先设置一个空的桌面为默认然后在将该空桌面禁用的方式来实现)
     *
     */
    @SuppressLint("WrongConstant")
    public void clearDefaultLauncher() {
        PackageManager pm = context.getPackageManager();
        String pn = context.getPackageName();
        String hn = MockHome.class.getName();
        ComponentName mhCN = new ComponentName(pn, hn);
        Intent homeIntent = new Intent("android.intent.action.MAIN");
        homeIntent.addCategory("android.intent.category.HOME");
        homeIntent.addCategory("android.intent.category.DEFAULT");
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pm.setComponentEnabledSetting(mhCN, 1, 1);
        context.startActivity(homeIntent);
        pm.setComponentEnabledSetting(mhCN, 0, 1);
    }

    //弹出选择默认桌面
    public void SetDefaultLauncher(){
        try {
            Intent  paramIntent = new Intent("android.intent.action.MAIN");
            paramIntent.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
            paramIntent.addCategory("android.intent.category.DEFAULT");
            paramIntent.addCategory("android.intent.category.HOME");
            context.startActivity(paramIntent);
        }catch (Exception e){
            e.printStackTrace();
        }

        try {
            Intent  paramIntent1 = new Intent("android.intent.action.MAIN");
            paramIntent1.setComponent(new ComponentName("com.huawei.android.internal.app", "com.huawei.android.internal.app.HwResolverActivity"));
            paramIntent1.addCategory("android.intent.category.DEFAULT");
            paramIntent1.addCategory("android.intent.category.HOME");
            context.startActivity(paramIntent1);

        }catch (Exception e){
            e.printStackTrace();
            startHuaweiSettingActOfDefLauncher();
        }


    }
    
    //打开华为设置页面,让用户选择默认桌面
    public void startHuaweiSettingActOfDefLauncher() {
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction(Intent.ACTION_MAIN);//"android.intent.action.MAIN"
        localIntentFilter.addCategory(Intent.CATEGORY_HOME);//"android.intent.category.HOME"
        Intent localIntent3 = new Intent(localIntentFilter.getAction(0));
        localIntent3.addCategory(localIntentFilter.getCategory(0));
        Intent localIntent4 = new Intent();
        localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent4.setClassName("com.android.settings", "com.android.settings.Settings$PreferredSettingsActivity");
        localIntent4.putExtra("preferred_app_package_name", context.getPackageName());
        localIntent4.putExtra("preferred_app_class_name", context.getClass().getName());
        localIntent4.putExtra("is_user_confirmed", true);
        localIntent4.putExtra("preferred_app_intent", localIntent3);
        localIntent4.putExtra("preferred_app_intent_filter", localIntentFilter);
        localIntent4.putExtra("preferred_app_label", "默认桌面设置");
        context.startActivity(localIntent4);
    }

    //获取当前默认桌面,如果不是本App则弹出设置默认桌面对话框
    public void getDefaultHome() {
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);
        if (res.activityInfo == null) {
            //获取失败
        } else if (res.activityInfo.packageName.equals("android")) {
            // No default selected
            clearDefaultLauncher();
            SetDefaultLauncher();
        } else {
            if (!res.activityInfo.packageName.equals("net.xiaomy.dxsjb.client")){
                clearDefaultLauncher();
                SetDefaultLauncher();
            }

        }
    }
}

这里注意:清除默认桌面,需要一个空的Activity,这里贴上xml文件代码,再自行添加一个Activity类就好

 <activity android:name=".activitys.MockHome" android:enabled="false"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

 

我这里在主Activity里调用:

 //设置默认桌面
        Desktop desktop=new Desktop(this);
        desktop.getDefaultHome();

好了,到这里就算完成了,欢迎留言评论交流,转载请注明出处!

 

自己新开发的免费内网穿透项目小蚂蚁内网穿透,支持HTTP、TCP全端口映射戳我进内网穿透官网www.xiaomy.net!

 

  • 12
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值