2014-10-30 78 views
0

如何處理任務:打開任務管理器以查看。在同一任務中啓動外部活動

我一直在想,自從很久以前就不可能在當前的任務中開始外部活動。但我發現這是不正確的!

我的應用程序可以在同一個任務中啓動一個twitter/facebook/sms(當點擊任務管理器按鈕時,我只能看到我的任務)。但是這不適用於瀏覽器。我已經嘗試過兩種瀏覽器應用程序Chrome和Firefox。沒有一個像twitter/facebook/sms一樣工作

以下是打開Facebook客戶端和瀏覽器的代碼。任何意見將非常感激!

public Intent shareViaFacebook(Activity ctx) { 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    ActivityInfo appInfo = searchForApp(ctx, intent, "facebook"); 

    if (appInfo == null) 
     return null; 

    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    intent.setComponent(new ComponentName(appInfo.packageName, appInfo.name)); 

    intent.putExtra(Intent.EXTRA_TEXT, "message"); 
    return intent; 
} 


public Intent launchBrowser(Activity ctx, String url) { 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 

    ActivityInfo appInfo = searchForApp(ctx, intent, null); 
    if (appInfo == null) 
     return null; 
    intent.setComponent(new ComponentName(appInfo.packageName, appInfo.name)); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    return intent; 
} 

回答

2

總結:啓動的外部活動是否可以在同一個任務中取決於它自己的聲明。

再舉一個例子,Android的瀏覽器應用程序聲明, Web瀏覽器的活動應在其自己的任務,通過指定 元素中的singleTask啓動模式始終打開。這意味着 如果您的應用程序發出打開Android瀏覽器的意圖,其 活動不會與您的應用程序處於同一任務中。相反, 要麼爲瀏覽器啓動新任務,要麼,如果瀏覽器已經有 任務在後臺運行,那麼該任務會提前處理 新的意圖。

從官方文檔。