2016-07-27 38 views
2

我有一個應用程序A和一個應用程序B,我試圖從應用程序A打開一個應用程序B的活動,這不是主要活動。爲什麼我無法使用軟件包名稱訪問我的活動?

我知道這個問題已經有很多答案,但我無法設法做出任何這些工作,這就是爲什麼我要求幫助。

首先這裏是應用B(即必須打開的)的「簡化」清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.rlaville.gestionsav" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Vues.CreationSav" 
      android:windowSoftInputMode="stateHidden" 
      android:noHistory="true"> 

     </activity> 
     <activity android:name=".Vues.GestionSav" android:noHistory="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Vues.StatSav" android:noHistory="true"> 

     </activity> 
     <activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" > 
      <action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </activity> 


     <provider 
      android:name=".Donnees.SavDataProvider" 
      android:authorities="com.example.rlaville.gestionsav.Donnees.SavDataProvider" 
      android:exported="true" 
      ></provider> 

    </application> 

</manifest> 

我想開是ModifierSav的活動,所以我也把一個意圖過濾器內正如它在每個教程中所說的那樣。

然後我試着在其他應用這些方法:

直接調用的意圖過濾器名稱:

Intent i = new Intent("com.example.rlaville.gestionsav.MODIFY_ACTIVITY"); 
    i.putExtra("CurrentSavID", itemId); 
    startActivity(i); 

或使用packageManager:

public boolean openSavApp(Context ctx, String packageName, long itemId){ 
    PackageManager manager = ctx.getPackageManager(); 
    Intent i = manager.getLaunchIntentForPackage(packageName); 
    if (i == null) { 
     return false; 
     //throw new PackageManager.NameNotFoundException(); 
    } 
    i.putExtra("CurrentSavID", itemId); 
    ctx.startActivity(i); 
    return true; 
} 

,我試着這樣的電話

openSavApp(getActivity(), "com.example.rlaville.gestionsav.MODIFY_ACTIVITY", itemId); 

我在做什麼錯?我應該怎麼做才能從另一個應用程序中打開這個特定的活動?

回答

3

您錯過了<intent-filter>標記。

<activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" > 
      <action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </activity> 

改變這

<activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" > 
<intent-filter> 
    <action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</activity> 

更新:

你越來越空的意圖,因爲你在這裏沒有提供正確的軟件包名稱。

你在這裏提供了你在manifest標籤中提到的這個包名。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.rlaville.gestionsav" >

public boolean openSavApp(Context ctx, String packageName, long itemId){ 
    PackageManager manager = ctx.getPackageManager(); 
    Intent i = manager.getLaunchIntentForPackage(packageName); 
    if (i == null) { 
     return false; 
     //throw new PackageManager.NameNotFoundException(); 
    } 
    i.putExtra("CurrentSavID", itemId); 
    ctx.startActivity(i); 
    return true; 
} 
+0

哇我總是犯最愚蠢的錯誤...非常感謝您的幫助 – RiddlerNewComer

+0

但是,當我打電話給openSavApp(getActivity(),「com.example.rlaville.gestionsav.MODIFY_ACTIVITY」,itemId);我的意圖仍然是空的,即使有意圖過濾器 – RiddlerNewComer

+0

@RiddlerNewComer你想通過調用'openSavApp(getActivity(),「com.example.rlaville.gestionsav.MODIFY_ACTIVITY」,itemId);'方法?你想重新啓動應用程序? –

2
<intent-filter> 
     <action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 

你忘了把行動和類別內intent-filter塊。

相關問題