Android安卓-开发一个android桌面

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

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

Android安卓-开发一个android桌面_第1张图片

 

1.添加XML

  
            
                
                
                
                
            
        

在主Activity里 添加


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

2.主Activity布局文件

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



    
        
    
    

3.获取APP列表

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

public static List GetAppList1(Context context){
        List list=new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List 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 appInfos=new ArrayList<>();

    public DeskTopGridViewBaseAdapter(List 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的布局文件




    
    

 

 

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

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


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

//桌面相关操作

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里调用:

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

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

 

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

 

你可能感兴趣的:(Android安卓-开发一个android桌面)