2017-12-12 46 views
0

私たちは、2つのアプリケーション画面のショートカットを有効にしました。マニフェストを使用して、以下のショートカットを参照しているActivityを初期化しました。ショートカットとランチャーウィジェット(Android)

<activity 
    android:name=".ui.shortcuts.ShortCut1" 
    android:screenOrientation="portrait" 
    android:icon="@drawable/shortcut1" 
    android:label="@string/app_shortcut_name1" 
    android:theme="@style/AppLightTheme"> 
     <intent-filter> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
     </intent-filter> 

</activity> 

コードから私は以下のようにショートカットを有効にしました。ノヴァとアクションランチャーで今

Intent shortcutIntent = null; 
shortcutIntent = new Intent(ApplicationNekt.getContext(), ShortCut1.class); 

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Intent intent = new Intent(); 
intent.putExtra("duplicate", false); 
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 



intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ApplicationNekt.getContext().getString(R.string.app_shortcut_name1)); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(ApplicationNekt.getContext(), R.drawable.shortcut1)); 

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
ApplicationNekt.getContext().sendBroadcast(intent); 

、彼らは私がマニフェストに与えたアイコンとテキストでショートカットセクションの下にショートカットが表示されます。クリックして保持すると、アイコンをホームタブに置くことができます。その直後に、ターゲットアクティビティが開きます。しかし、私が電話機のホーム画面に戻ると、前の手順で作成したショートカットアイコンが削除されました。

ここに何か不足していますか?

答えて

0

NovaランチャーのKevinさんがサポートメールに返信しました。

そのまた、私の場合は別のスレッドでAndroid define shortcut that can be used in custom launcher

を説明し、私は両方のショートカットがコードを追加するだけでなく、私はノヴァ/アクションランチャーのウィジェット画面からショートカットを追加したいユーザーをサポートする必要があります。だから私は以下のことをしました。

以下のコード私はShortCut1.javaクラスファイルに書きました。これが活動コードです。

public class ShortCut1 extends Activity{ 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // This code runs when the user actually clicks and 
    // opens the shortcut. so redirect him to target screen. 
    openTargetTab(0); 


    // This code is useful when called by the Nova/Action launcher's 
    // widget is clicked. So return them with icon, name and target 
    // activity. Once they receive it they will set the short cut icon on home. 
    // Note: Even when the shortcut is clicked, this result is set, 
    // but nobody reads the response. So it should be ok. 
    Intent resIntent = getResIntent(); 
    setResult(RESULT_OK, resIntent); 

    finish(); 
} 

private Intent getResIntent() { 

    Intent shortcutIntent = new Intent(); 
    // Target intent is set to this own class. So that when the user clicks on the shortcut this intent will be passed. 
    Intent target = new Intent(this, ShortCut1.class); 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, target); 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
     Application.getContext().getString(R.string.shortcut_name)); 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
     Intent.ShortcutIconResource.fromContext(Application.getContext(), 
               R.drawable.shortcut1)); 
    return shortcutIntent; 
} 

private void openHomeTab(int tabIndex) { 

    // Final target screen. 
    Intent intent = new Intent(this, TargetActivity.class); 
    startActivity(intent); 
} 
} 

注:マニフェストまたはショートカット追加コードでコードを削除したり変更したりしていません。私のアプリでもそのサポートが必要なので、私はそのコードをそのまま残しました。ユーザーが「ショートカットを追加」をクリックすると、そのコードが実行されます。私がここで行った変更は、第三者のランチャーが理解できる適切な意図で「Setresult」と呼ばれています。

関連する問題