How to get all homescreens in Android?

后端 未结 1 591
北海茫月
北海茫月 2021-01-26 14:38

I am new to android development. I know that every homescreen is a Workspace in Launcher. I want to get all the position info of all application icons on the screen, so is there

相关标签:
1条回答
  • 2021-01-26 15:14

    first create a bean to store app info like this

    public class Bean {
    
     String appName;
     String packName;
     Drawable icon;
     Date installTime;
     Date updateTime;
     int size;
    }
    

    now create four list here for ApplicationInfo,results,packname,bean.

    public class AllApplications extends Activity  {
    
    List<ApplicationInfo> AppList;
    private ArrayList<String>results;
    private ArrayList<String>packName;
    public ArrayList<Bean> list;
    ProgressDialog progress;
    ActivityManager am ;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_applications);
    
    
        try 
        {
            new LoadApps().execute();
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("Exception",e.toString());
        }
    
    }
    
    
    class LoadApps extends AsyncTask<Void, Void,String>
    {
    
        @Override 
        protected void onPreExecute() { 
            progress= ProgressDialog.show(AllApplications.this,"Loading",
                    "Please Wait");
        } 
        @Override
        protected String doInBackground(Void... params) 
        {
            PackageManager pm = AllApplications.this.getPackageManager();
            list = new ArrayList<Bean>();
            AppList = new ArrayList<ApplicationInfo>();
            AppList = pm.getInstalledApplications(0);
            Log.e("Package info",""+AppList);
            results = new ArrayList<String>();
            packName = new ArrayList<String>();
            Log.e("Total package list",""+AppList.size());
    
            for(int i=0;i<AppList.size();i++)
            {
                Bean bean = new Bean();
                ApplicationInfo appInfo  = AppList.get(i); 
                try 
                {
                    PackageInfo packInfo = pm.getPackageInfo(appInfo.packageName,0);
                    try 
                    {
                        java.lang.reflect.Field field1 = PackageInfo.class.getField("firstInstallTime");
                        long firstIns = field1.getLong(packInfo);
                        Date insDate = new Date(firstIns);
                        bean.installTime = insDate;
                        java.lang.reflect.Field field2 = PackageInfo.class.getField("lastUpdateTime");
                        long lastUpdate = field2.getLong(packInfo);
                        Date upDate = new Date(lastUpdate);
                        bean.updateTime = upDate;
    
                    } 
                    catch (NoSuchFieldException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
    
                } 
                catch (NameNotFoundException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.e("ins package", "" + appInfo.packageName);
                Log.e("ins package name", "" +appInfo.loadLabel(pm));
                Log.e("launched activity :", "" + pm.getLaunchIntentForPackage(appInfo.packageName)); 
                bean.appName = (String) appInfo.loadLabel(pm);
                bean.packName = appInfo.packageName;
                bean.icon = appInfo.loadIcon(getPackageManager());
                list.add(bean);
                //              PackageInfo. this.firstInstallTime(a);
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
    
            progress.dismiss();
    
        }
    
    }}
    

    Try this it ll help you alot,and let me know for more help.

    0 讨论(0)
提交回复
热议问题