2014-02-15 18 views
4

此應用程序的任務是隱式激活一個單獨的應用程序來查看URL「http:// www.google.com」。所以,應用選擇器應該出現,並讓我至少在兩個瀏覽器中進行選擇:默認的瀏覽器會顯示www.google.com網站,另一個簡單的「瀏覽器」會向我顯示網址。 問題是 - 應用程序選擇器不會出現,當我使用隱式活動。可能我爲第二個「簡單瀏覽器」寫了錯誤的意圖過濾器。如何啓動和應用選配器

private void startImplicitActivation() { 

    Log.i(TAG, "Entered startImplicitActivation()"); 

    // TODO - Create a base intent for viewing a URL 
    // (HINT: second parameter uses parse() from the Uri class) 
    Uri adress = Uri.parse(URL); 
    Intent intentUrlView = new Intent(Intent.ACTION_VIEW, adress); 

    // TODO - Create a chooser intent, for choosing which Activity 
    // will carry out the baseIntent. Store the Intent in the 
    // chooserIntent variable below. HINT: using the Intent class' 
    // createChooser()) 
    Intent chooserIntent = Intent.createChooser(intentUrlView, CHOOSER_TEXT); 
    //chooserIntent = null; 

    Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction()); 
    // TODO - Start the chooser Activity, using the chooser intent 
    if (intentUrlView.resolveActivity(getPackageManager()) != null) { 
     startActivity(chooserIntent); 
    } 
} 

清單

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="course.labs.intentslab.mybrowser" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="13" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MyBrowserActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

      <!-- TODO - Add necessary intent filter information so that this 
          Activity will accept Intents with the 
          action "android.intent.action.VIEW" and with an "http" 
          schemed URL --> 
     <action android:name="android.intent.action.VIEW" /> 
     <data android:scheme="http" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     </activity> 
    </application> 

+5

所以這是來自馬里蘭州Android課程第一部分coursera ... –

回答

8

嚴重的是,你有一個清單的GUI編輯器。

<action android:name="android.intent.action.VIEW" /> 
<data android:scheme="http" /> 
<category android:name="android.intent.category.BROWSABLE" /> 

應該放在<intent-filter>標籤,因爲你已經可以在你的清單中看到。 像這樣:

<activity 
    android:name=".MyBrowserActivity" 
    android:label="@string/app_name" > 

    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 

     <!-- TODO - Add necessary intent filter information so that this 
         Activity will accept Intents with the 
         action "android.intent.action.VIEW" and with an "http" 
         schemed URL --> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <data android:scheme="http" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
    </intent-filter> 

</activity> 

也就是說它在默認瀏覽器如何做:

 <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
      <data android:scheme="about" /> 
      <data android:scheme="javascript" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
      <data android:scheme="inline" /> 
      <data android:scheme="file" /> 
      <data android:mimeType="text/html" /> 
      <data android:mimeType="text/plain" /> 
      <data android:mimeType="application/xhtml+xml" /> 
      <data android:mimeType="application/vnd.wap.xhtml+xml" /> 
     </intent-filter> 
+0

謝謝vety很多!由於疲倦,我沒有認識到這種愚蠢的錯誤。但是,即使在這之後,我的程序仍然無法正常工作。 – Mabjik

+1

如果您包含類別,所有匹配intent-filter **的意圖都必須**設置此類別。您的代碼未設置所需的類別,因此您的意圖與您的過濾器不匹配。閱讀文檔中意圖過濾器的工作原理 – nekavally

+0

「只有意圖可以通過您的意向過濾器之一,系統纔會向您的應用組件提供隱含意圖。」 - http://developer.android.com/intl/ru/guide/components/intents-filters.html據我所知,如果一個應用程序通過和動作意圖過濾,那麼它會在App Chooser中出現。但我刪除了類別字段,我查看了文檔,並找不到清單文件中的問題。現在我確信問題出在該瀏覽器應用程序中。我已經安裝了opera mini和chrome,並且我看到了App選擇器,包含它們,但不是那個應用程序。 – Mabjik

0

以下是我正在做的:

步驟1:首先在弦,

<string name="webklink">&lt;a href="http://www.google.com">SomeLink&lt;/a></string> 

Ste p 2:我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/webklink" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

第3步:從字符串獲取鏈接。

public class OpenWeb extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webopen); 

     TextView someLink = (TextView)findViewById(R.id.textView1); 
     if(someLink!=null){ 

      someLink.setMovementMethod(LinkMovementMethod.getInstance()); 
      someLink.setText(Html.fromHtml(getResources().getString(R.string.webklink))); 
     } 

    } 
} 

希望這有助於.. :)

+0

我不明白爲什麼選擇上面的答案。這非常好,它按照每個問題的要求運行良好。如果有人正在做一個downvote,應該在評論框中解釋一個原因。 – mike20132013

18

好像的亞當波特的第三個星期分配的編程Android手持系統移動應用程序。 無論如何,我希望你的解決方案 在AndroidManifest.xml中,你需要添加三個意圖過濾器。

<action android:name="android.intent.action.VIEW" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data android:scheme="http" /> 

此外,如果你不能夠運行AndroidUnit測試,

確保你已經在隱含按鈕調用的ActionListener做一些與此類似。

Intent baseIntent = new Intent(Intent.ACTION_VIEW); 
     baseIntent.setData(Uri.parse(URL)); 

Intent chooserIntent = Intent.createChooser(baseIntent, "Select Application"); 

Coursera已經有一個郵件列表。請使用它。

+6

+1「看起來像Adam Porter爲Android HandHeld系統編程的移動應用程序的第三週分配」:) – radistao

0

易於實施的多個動作的應用程序選擇器的:

Intent phoneCall=new Intent(Intent.ACTION_DIAL,Uri.fromParts("tel",contactNumber,null)); 
      //Intent sms=new Intent(Intent.ACTION_VIEW,Uri.parse("sms:"+contactNumber)); 
      Intent whatsapp=new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+contactNumber)); 
      Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); 
      builder.appendPath("time"); 
      ContentUris.appendId(builder, Calendar.getInstance().getTimeInMillis()); 
      Intent calendarIntent = new Intent(Intent.ACTION_VIEW).setData(builder.build()); 

      Intent chooser=Intent.createChooser(whatsapp,"chooser??");//default action 
      chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{phoneCall,calendarIntent});//additional actions 
      startActivity(chooser); 
0

你必須運行lab3a_MyBrowser運行lab3a_IntentsLab之前,讓瀏覽器獲得您的手機上安裝。然後它將對您的選擇器可見。

相關問題