2013-05-14 52 views
7

我創造了Android 4.0以上版本的應用程序,我想實現這種行爲。當用戶的設備是手機時,兩種不同的活動將具有兩個啓動器圖標,並且在平板設備上它將只是一個(活動將包含片段,我將在一個主要活動中顯示爲製表符)。我知道可以在清單中設置多個啓動器活動,但我認爲也許我需要一些能夠在運行時(在java代碼中)確定此操作的東西。兩個在電話發射活動,一個在平板

回答

0

您將需要進行多個APK,一個標籤,一個用於手機,唯一的區別是在那些的APK清單文件。您可以使用代碼進行管理。

0

爲什麼採取這麼多的痛苦?只需要爲手機和平板電腦設計單獨的應用程序(確保它們具有相同的軟件包名稱)。 Multiple APKs是你應該尋找什麼:

Although we encourage you to develop and publish a single APK that supports as many device 
configurations as possible, doing so is sometimes not possible. To help you publish your 
application for as many devices as possible, Google Play allows you to publish multiple 
APKs under the same application listing. Google Play then supplies each APK to the 
appropriate devices based on configuration support you've declared in the manifest file of 
each APK. 

這裏有一個鏈接:http://developer.android.com/google/play/publishing/multiple-apks.html

+0

我已經開始發展我與手機和平板電腦的單個APK應用程序。我的洞應用邏輯就是基於此。現在我需要添加新的活動,這種行爲。所以我需要......可以讓我的活動 - 代碼中的啓動器活動:s – Sandra 2013-05-14 09:59:56

+0

只使用一個活動啓動器,並在setContent之前的onCreate中檢查設備是手機還是平板電腦(通過計算屏幕大小)。之後,將所需的xml設置爲活動的內容。 – 2013-05-14 10:06:17

+0

Thx的建議,但我已經在我的應用程序中使用。現在,我需要在平板電腦上顯示片段作爲選項卡,並將其封裝在手機上的單獨啓動器活動中。我認爲必須有辦法做到這一點。 – Sandra 2013-05-14 10:42:54

12

兩個不同的活動,將有兩個發射器圖標,當用戶的設備是手機,這將只是一個(在活動將由片段設備組成,我將在一項主要活動中顯示爲標籤)。

Android中沒有「手機」和「平板電腦」的概念。我將假設您在屏幕尺寸方面區分「手機」和「平板電腦」。

如果這是真的:

第1步:創建一個res/values/bools.xml文件,並定義了兩個<bool>資源,is_phoneis_tablet。有is_phonetrueis_tabletfalse

第2步:創建一個res/values-.../bools.xml文件,其中...是你與你的佈局使用「手機」和「平板電腦」之間的區分什麼預選賽(例如,-large-xlarge-swNNNdp)。具有相反的值(即,is_phonefalseis_tablettrue)定義相同的兩個<bool>資源那裏。

步驟#3:將兩個活動添加到您的清單,每個設置爲MAIN/LAUNCHER<intent-filter>。在您想要在「手機」上使用的那個上,將android:enabled="@bool/is_phone"添加到<activity>元素。在要用於「平板電腦」的那個上,將android:enabled="@bool/is_tablet"添加到<activity>元素。

這種方式,根據您正在使用您的佈局相同的規則,你將有一個不同的發射活動。


顯然,這是不行的,但我發誓用。

另一種選擇是有一個單一的活動是MAIN/LAUNCHER之一。它的設置與android:theme="@style/Theme.NoDisplay",所以它沒有一個用戶界面。讓它以Java的方式在onCreate()中確定哪個「真正」的入口點活動適合給定的屏幕大小,也許使用上面引用的相同的bool資源。然後讓它調用startActivity()將控制權傳遞給正確的活動調用finish()(因此用戶在BACK堆棧中不會遇到不可見活動)。這種技術也用於無法通過清單控制這種情況的情況,例如「我們是否有Maps V1」,而您在<uses-library>元素上有android:required="false"

+0

這似乎是一個有趣的解決方案。我不知道可以在清單中爲活動設置的「已啓用」屬性。我一定會嘗試一下。謝謝你! – Sandra 2013-05-15 08:22:20

+0

我按照你的建議做了,但是......不適合我。由於上述行爲我想實現,我在bools.xml文件中設置了兩個資源(res/values中的bools.xml和res/values-sw600dp中的bools.xml)=> main_activity_enabled(true在res /值res-values-sw600dp中的值),second_activity_enabled(res/values中爲true,res/values-sw600dp中爲false)。從這我應該只有MainActivity作爲啓動器在平板設備上,但儘管我有兩個發射器:S – Sandra 2013-05-15 09:34:47

+0

@Sandra:您需要'main_activity_enabled'在您的某個案例中爲'false' - 您聲稱它是'真正的「。 – CommonsWare 2013-05-15 10:35:16

1

第一次啓動應用程序,您可以通過編程檢查你的設備是平板電腦或手機。如果它是手機,則可以通過編程的方式向主屏幕添加快捷方式,並指定啓動程序應使用的Intent,以便轉到正確的活動。

首先,在你的Manifest中,你將只有一個啓動器,但是你將有一個沒有意圖過濾器的Activity,它們將通過編程創建的快捷方式打開。

接下來,在您的啓動程序Activity的onCreate中,檢查您是否已經創建了快捷方式。從我已閱讀,要做到這一點的最好辦法是使用SharedPreferences一個布爾值:

SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE); 
if (!prefs.getBoolean("shortcut", false)) { 
    if (!isTablet(this)) { 
     createShortcut(); 
    } 
    prefs.edit().putBoolean("shortcut", true).commit(); 
} 
else { 
    //if the shortcut has already been made, see if that is what was used to open this app 
    if (getIntent() != null) { 
     if (getIntent().getBooleanExtra("shortcut", false)) { 
      //the shortcut was used, so open the other Activity: 
      Intent shortcut = new Intent(this, OtherActivity.class); 
      startActivity(shortcut); 
     } 
    } 
} 

接下來,你需要定義你所謂的平板電腦。這是基於屏幕上與密度無關的像素。例如,調用與7英寸的屏幕片劑設備,此數是600。對於較大的設備 - 諸如一個10英寸的屏幕,這個數目是720存儲該號碼在一個變量:

private static final int TABLET_DP = 600; 

然後上面添加使用的方法:

public static boolean isTablet(Context context) { 
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);\ 
    Display display = wm.getDefaultDisplay(); 
    DisplayMetrics outMetrics = new DisplayMetrics(); 
    display.getMetrics(outMetrics); 
    float density = context.getResources().getDisplayMetrics().density; 
    float dpWidth = outMetrics.widthPixels/density; 
    return dpWidth >= TABLET_DP; 
} 

private void createShortcut() { 

    //this is the intent that will be used when the shortcut is clicked. 
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); 
    shortcutIntent.setClassName(this, this.getClass().getName()); 
    shortcutIntent.putExtra("shortcut", true); 

    //this is the intent used to create the shortcut. 
    Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));//or however you want the shortcut to be labeled. 
    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
      this, R.drawable.ic_launcher);//or whatever you want the icon to be 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 

    //tell Android to create the shortcut 
    context.sendBroadcast(intent); 
} 

最後,要確保你有正確的權限來安裝快捷:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"></uses-permission> 
相關問題