如何在Android中获取所有主屏幕?

时间:2014-05-15 04:30:42

标签: android launcher homescreen

我是Android开发的新手。我知道每个主屏幕都是Launcher中的工作区。我想获得屏幕上所有应用程序图标的所有位置信息,那么有没有办法获取这些屏幕对象及其图标信息的列表?

ADD:我期望更多的是app图标和屏幕之间的关系。例如,我想要某个应用程序图标的位置信息,包括它出现在哪个屏幕上(即屏幕列表的索引)。

1 个答案:

答案 0 :(得分:0)

首先创建一个bean来存储像这样的应用信息

public class Bean {

 String appName;
 String packName;
 Drawable icon;
 Date installTime;
 Date updateTime;
 int size;
}

现在为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();

    }

}}

试试这个它会帮助你很多,并让我知道更多的帮助。