簡體   English   中英

快捷方式添加到Android主屏幕

[英]Shortcut Add to Android Home Screen

我該如何做與電報和許多其他應用類似的操作,在這種情況下,您可以通過電報添加一個元素,如果單擊該元素可以打開聯系人聊天窗口。

我想做這樣的事情,如果您在首頁上添加一個元素,則可以執行某些操作。

但是我必須打開一個我的外部應用程序。

編輯:

單擊主屏幕上str名稱連接元素上的鏈接時必須調用的Intent。

Intent appIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/"+str));
appIntent.setPackage("com.instagram.android");
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/"+str));
 try {
     startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
    startActivity(webIntent);
}

EDIT2:

加:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

碼:

if (ShortcutManagerCompat.isRequestPinShortcutSupported(getBaseContext())) {
Intent instagramIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/_u/" + str));
instagramIntent.setPackage("com.instagram.android");
Bitmap bmp = getCroppedBitmap(bitmap);
final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bmp) : IconCompat.createWithBitmap(bmp);
final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getBaseContext(), UUID.randomUUID().toString())
                        .setShortLabel(str)
                        .setLongLabel(str)
                        .setIcon(icon)
                        .setIntent(instagramIntent)
                        .build();
ShortcutManagerCompat.requestPinShortcut(getBaseContext(), shortcut, null);
            }

如果我理解正確,則希望從應用程序創建的快捷方式啟動應用程序。 然后,您可以執行以下操作:

public void createShortCut{
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
    sendBroadcast(shortcutintent);
}

檢查一下

編輯:

這是使用ShortcutManagerCompat做到這一點的最佳方法:

fun createShortCut(){
    val str= ""
    val uri = Uri.parse("http://instagram.com/_u/"+str)
    val instagramIntent = Intent(Intent.ACTION_VIEW,uri)
    instagramIntent.setPackage("com.instagram.android")
    val icon = IconCompat.createWithResource(this,R.drawable.ic_launcher_background)
    val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "shortcutID")
                .setIcon(icon)
                .setShortLabel("MyShortcut")
                .setIntent(instagramIntent)
                .build()
    ShortcutManagerCompat.requestPinShortcut(this,pinShortcutInfo,null)
}

不確定我是否了解您要完成的工作,但是可以。 您可以在清單中添加這樣的代碼

<activity
    android:name=".activityName"
    android:screenOrientation="sensorLandscape">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

使用intent-filter action.Main可以從主屏幕啟動您的活動。 因此,只需將該過濾器添加到要放在主屏幕上的活動中即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM