2013-03-13 2 views
5

그래서 내 URL이 내 앱에 호출되도록하려고하는데, 앱을 연 후에 URL은 항상 null 인 것 같습니다. 해야 할 것. 여기Android URL 구성표 : intent.getData() == null (PhoneGap)

<activity android:name="myapp" android:label="@string/app_name" 
      android:theme="@android:style/Theme.Black.NoTitleBar" 
      android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" 
      android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <intent-filter> 
      <data android:scheme="myapp" /> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
     </intent-filter> 

    </activity> 

그리고 내 자바는 다음과 같습니다 :

여기 내 매니페스트의 관련 부분

@Override 
public void onStart() { 

    super.onStart(); 
    final Intent intent = getIntent(); 

    Uri url = intent.getData(); 

    Log.e("myLog", "Url = " + url); 

} 

로그는 "의도 = 널 (null)"를 출력하고, 나는 오류가 없습니다.

나는 자바 개발자가 아니므로 인식하지 못하는 간단한 문제가있을 수 있습니다. sendJavascript으로 PhoneGap 앱에 이것을 전달할 예정이지만 PhoneGap 앱이이 문제와 관련없는 것으로 보입니다.

편집 :

내가 intent를 로그인 할 때 첫 번째 <intent-filter>을 받고있는 것으로 보인다

. 여기 로그입니다 :

03-13 12:36:18.593: E/MyAppLog(13793): Intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.wd.myapp/.myapp (has extras) } 

해결책 :

당신의 폰갭 앱을 실행하는 URL 체계를 구현하고, 그들 중 누구도 매우 강력한 존재하지 않거나 전혀 작동하는 방법에 대한 게시물을 통해 수십 쏟아져 후

, 내 전체 솔루션을 게시하고 누군가가 유용하다고 생각하기를 바랍니다.

1 단계 : AndroidManifest.xml - <activity><intent-filter>을 추가하고 'urlScheme'을 원하는대로 변경하십시오. 이 경우 urlScheme : ///? key = val 링크를 클릭하면 앱이 열립니다.

<intent-filter> 
    <data android:scheme="urlScheme" /> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
</intent-filter> 

2 단계 : myApp.java이 - onCreate 아래에 다음을 추가합니다. singleTask을 사용하는 경우 로직을 함수 (onCreate 대신)에 넣어야합니다. 다른 사람이 트럼펫을 사용함에 따라) 이미 실행 중인지 여부에 관계없이 앱에 연결할 수 있습니다.

@Override 
public void onStart() { 

    super.onStart(); 
    final Intent intent = getIntent(); 

    String url = intent.getDataString(); 

    if(url != "" && url != null) { 
     super.sendJavascript("setTimeout(function() { handleOpenURL('" + url + "'); },1);"); 
    }     


} 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); setIntent(intent); 
} 

onNewIntent 대체 설명에 대한 대답을 참조하십시오.

+0

나는 생각한다. 최종 의도 Intent = getIntent(); final이어야합니다. Intent intent = intent.getIntent(); –

+0

어떻게 그렇게? '의도'는 아직 존재하지 않습니다. 나는 그것을 시험해 볼 것이다. – bearfriend

+0

그래, 그건 방법이 아니야. 나는'의도 '를 그대로 기록했고 그걸로 뭔가를 얻었다고 믿는다. 그것은 그것의 데이터를 얻기 위해 그것의 메소드를 호출하는 것입니다. 항상 null입니다. – bearfriend

답변

14

onNewIntent을 무시하십시오. 문제는 getIntentActivity을 시작했지만 가장 최근의 것이 아닌 Intent을 반환한다는 것입니다. 하지만 setIntent 메서드를 사용하여 재정의 할 수 있습니다.

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    setIntent(intent); 
} 
+0

감사는 매력처럼 작동합니다. 내 해결책에 대해서만해야합니다. 내가 시작한 새로운 활동은 내 주요 활동에서 ActivityZ를 내 onStart 메소드로 말합니다. 그러나 내가 다시 주요 활동으로 돌아올 때마다 ActivityZ가 다시 시작될 때마다 무슨 일이 일어나고 있었는지, 그래서 의도 한 데이터를 명시 적으로 null로 설정하는 것입니다. – cocoaNoob

0

주요 활동이 다른 활동을 시작하는 경우 확장합니다. 그러면 시작한 활동의 ​​주요 활동으로 돌아갈 때마다 활동을 반복해서 시작하는 무한 루프를 끝낼 수 있습니다.

여기

public void onStart(){ 
     super.onStart(); 
     Intent intent = getIntent(); 
     Uri data = intent.getData(); 
     if (data != null) { 
      // explicitly set intent data to null 
      // so on next launch, from same lifecycle of activity 
      // it does not restart the Activity Z. 
      intent.setData(null); 
      Intent activityZ = new Intent(MyActivity.this, ActivityZ.class); 
       businessViewActivity.putExtra("url", data.toString()); 
       startActivity(activityZ); 
     } 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     setIntent(intent); 
    } 

는 희망이 도움이 코드입니다.

+0

AndroidManifest.xml의 활동에 android : launchMode = "singleTask"를 설정해야합니다 (질문의 코드 참조) – jackocnr