2012-11-13 36 views
1

我試圖在Android中創建自己的啓動器,並使用android sample HomeAndroid,啓動器和最喜歡的應用程序

該示例不是很容易,幾乎沒有關於它的文檔或教程,只是在論壇上的一些問題沒有明確的答案。

我想在我的程序添置喜愛的應用程序,但應用程序尋找一個xml文件「etc/favorites.xml」,這不存在。

我是否必須以編程方式創建此文件?這個文件是什麼樣的?

回答

1

我找到了解決方案。

我創建的文件夾在文件「favorites.xml」,「資產」,寫:

<?xml version="1.0" encoding="UTF-8"?> 

<favorites> 
    <favorite package="com.android.email" class="com.android.email.activity.Welcome"/> 
    <favorite package="com.android.browser" class="com.android.browser.BrowserActivity"/> 
</favorites> 

而在示例代碼,我編輯方法「bindFavorites」(I與加載XML文件InputStream的代替的FileReader):

/** 
* Refreshes the favorite applications stacked over the all apps button. 
* The number of favorites depends on the user. 
*/ 
private void bindFavorites(boolean isLaunching) { 
    if (!isLaunching || mFavorites == null) { 

     if (mFavorites == null) { 
      mFavorites = new LinkedList<ApplicationInfo>(); 
     } else { 
      mFavorites.clear(); 
     }   

     InputStream is = null; 
     try { 
      is = getAssets().open("favorites.xml"); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Couldn't find or open favorites file "); 
      return; 
     } 

     final Intent intent = new Intent(Intent.ACTION_MAIN, null); 
     intent.addCategory(Intent.CATEGORY_LAUNCHER); 

     final PackageManager packageManager = getPackageManager(); 

     try { 
      final XmlPullParser parser = Xml.newPullParser(); 
      parser.setInput(is, "UTF-8"); 

      beginDocument(parser, TAG_FAVORITES); 

      ApplicationInfo application; 

      while (true) { 
       nextElement(parser); 
       String name = parser.getName(); 
       if (!TAG_FAVORITE.equals(name)) { 
        break; 
       } 

       final String favoritePackage = parser.getAttributeValue(null, TAG_PACKAGE); 
       final String favoriteClass = parser.getAttributeValue(null, TAG_CLASS); 

       final ComponentName cn = new ComponentName(favoritePackage, favoriteClass); 
       intent.setComponent(cn); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       application = getApplicationInfo(packageManager, intent); 
       if (application != null) { 
        application.intent = intent; 
        mFavorites.addFirst(application); 
       } 
      } 
     } catch (XmlPullParserException e) { 
      Log.w(LOG_TAG, "Got exception parsing favorites.", e); 
     } catch (IOException e) { 
      Log.w(LOG_TAG, "Got exception parsing favorites.", e); 
     } 
    } 

    mApplicationsStack.setFavorites(mFavorites); 
} 

和它的作品,但我仍然需要一些幫助,在XML文件中,我們必須設置類值,我不知道在哪裏可以找到這些信息。正如你所看到的,這個值取決於應用程序。我在這裏發現了一些值:http://forum.xda-developers.com/showthread.php?t=836719 但我有我自己的鈦應用程序,我不知道需要哪個類的價值。

+0

Re,我忘了更新我的文章,我發現我想要的一切。如果這是你自己的應用程序,這很容易。你只需要你的packageName「com.example.yourApp」和你的className「MainActivity」,例如:。 – Aximem

相關問題