Androidは、ホーム画面にショートカットを作成します
-
27-10-2019 - |
質問
私がやりたいのは:
1)私はアクティビティの中にあり、2つのボタンがあります。最初のものをクリックすると、ホーム画面にショートカットが作成されます。ショートカットが開きます html
以前にダウンロードされたページなので、デフォルトのブラウザを使用したいのですが、インターネットを使用したくありません。
2)2番目のボタンは、アクティビティを開始する別のショートカットを作成します。そして、私はアクティビティにいくつかの追加の議論を渡したいと思っています(例えば文字列として)...........
それらは可能ですか?リンクや同様の質問を見つけました Android:ホーム画面にWebショートカットを作成するプログラミング方法はありますか
彼らは私の質問に対する答えのようですが、誰かがこのコードはすべてのデバイスで動作するのではなく、それが非推奨であり、私がやりたいことは不可能だと言っていました.......
この手法は推奨されません。これは内部実装であり、Android SDKの一部ではありません。すべてのホーム画面の実装では機能しません。 Androidの過去のすべてのバージョンでは機能しない場合があります。 Googleは内部の文書化されていないインターフェイスを維持する義務がないため、将来のバージョンのAndroidでは機能しない可能性があります。これを使用しないでください
内部実装とはどういう意味ですか?そのコードは信頼できるかどうか.....私を助けてくださいpls .....
解決
サンプルコードは、文書化されていないインターフェイス(許可と意図)を使用してショートカットをインストールします。 「誰か」があなたに言ったように、これはすべての電話で機能しない可能性があり、将来のAndroidリリースで壊れる可能性があります。それをしないでください。
正しい方法は、ホーム画面からのショートカットリクエストを聞くことです。
<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
次に、意図を受け取るアクティビティでは、ショートカットの意図を作成し、アクティビティの結果として返します。
// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intent = new Intent();
Intent launchIntent = new Intent(this,ActivityToLaunch.class);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
setResult(RESULT_OK, intent);
他のヒント
Android Homescreen [自分のアプリでテスト]でショートカットアイコンを作成するための1つの方法を以下に開発しました。それを呼ぶだけです。
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
アクティビティ名、アイコンリソース、許可を変更することを忘れないでください。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
ハッピーコーディング!!!
編集:
重複する問題については、最初のオプションは、コードに以下の行を追加することです。
addIntent.putExtra("duplicate", false);
2番目のオプションは、最初にアプリのショートカットアイコンをアンインストールし、最初のオプションが機能しなかった場合に再度インストールすることです。
com.android.launcher.action.install_shortcutブロードキャストは、Android oreo以来何の効果もありません。 リンク
すべてのAndroidバージョン、特にサポートしたい場合 Android 8.0またはOreoおよびNewer, 、以下のコードを使用して、ショートカットを作成します。
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
マニフェストファイルに許可を追加します。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
上記の解決策を少し改善しました。これで、ショートカットが既に追加されているかどうかが設定を保存し、ユーザーが削除した場合、アプリの新しい起動に追加されません。既存のショートカットを追加するコードが実行されなくなったため、これにより少し時間が節約されます。
final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
// Creates shortcut on Android widget screen
private void createShortcutIcon(){
// Checking if ShortCut was already added
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
if (shortCutWasAlreadyAdded) return;
Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
// Remembering that ShortCut was already added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
editor.commit();
}
Android Oから始めて、これがショートカットを作成する方法です。
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setShortLabel(label)
.setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
.build();
shortcutManager.requestPinShortcut(pinShortcutInfo, null);
}
悲しいことに、多くの制限があります:
- ユーザーに追加を受け入れる必要があります。
- バックグラウンドで追加/削除することはできません。
- あなたはそれを更新できるはずですが、私にとってはうまくいきませんでした。
- ターゲットを絞ったアプリが削除された場合、削除されません。それを作成したものだけ。
- 現在のアプリの場合を除き、ターゲットアプリのリソースに基づいてアイコンを作成できません。ただし、ビットマップから実行できます。
Pre-android Oの場合、ショートカットマンアガーコンパートを使用して、これらの制限なしに新しいショートカットも作成できます。
以来 API level 26
, 、使用 com.android.launcher.action.INSTALL_SHORTCUT
非推奨です。ショートカットを作成する新しい方法が使用されています ShortcutManager
.
静的、動的、ピン留めの3種類のショートカットがあることに言及しています。要件に基づいて、それらを作成することを選択できます。
@siddiq abu bakkarの回答作品。ただし、アプリが起動するたびにショートカットの作成を防ぐために、共有設定を使用します。
final String PREF_FIRST_START = "AppFirstLaunch";
SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
if(settings.getBoolean("AppFirstLaunch", true)){
// record the fact that the app has been started at least once
settings.edit().putBoolean("AppFirstLaunch", false).commit();
ShortcutIcon();
}
final Intent shortcutIntent = new Intent(this, SomeActivity.class);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
私はそれを使用しましたが、ホーム画面の一部のデバイスには2つのアイコンが追加されます。私は理解していなかった??