kga*_*oid 5 android android-shortcut
从应用商店下载应用时,默认情况下,iOS 在快捷方式选项中提供“共享应用名称”选项。
请参考下图:
当单击它时,它将在电话菜单屏幕本身中打开共享意图,而无需重定向到应用程序,用户可以在其中共享应用程序。
我想在android中实现它。
到目前为止,这是我尝试过的:
<shortcut
android:shortcutId="share_app"
android:enabled="true"
android:icon="@drawable/ic_cart_active"
android:shortcutShortLabel="@string/shortcut_share"
android:shortcutLongLabel="@string/shortcut_share">
<intent
android:action="android.intent.action.send"
android:targetPackage="com.example.android.internal"
android:targetClass="" />
</shortcut>
Run Code Online (Sandbox Code Playgroud)
但这是行不通的,因为我无法理解,这里的targetClass应该是什么 。
编辑: Nilesh的答案几乎奏效,但是我现在面临的唯一问题是,每当我从快捷方式单击共享按钮时,启动器活动就会显示一秒钟,然后显示共享意图选择器现在,当我按下显示共享选项的主屏幕按钮时,该应用将进入后台。这不应该发生。任何想法如何避免这种情况。
您可以使用App shortcuts
但这个选项的主要缺点是它可以从 Oreo 及以上版本的 android 中获得
这是示例代码
创建资源文件:
res/xml目录(截图)
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_share"
android:shortcutDisabledMessage="@string/share_app"
android:shortcutId="nilu"
android:shortcutLongLabel="@string/share_app"
android:shortcutShortLabel="@string/share_app">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="neel.com.demo.ShareActivity"
android:targetPackage="neel.com.demo" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
Run Code Online (Sandbox Code Playgroud)
现在您需要在清单文件中的活动标签下注册您的快捷方式
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="neel.com.demo">
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".ShareActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
现在创建一个简单的共享活动来共享您的应用程序链接
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class ShareActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"Hey check out my app at: https://play.google.com/store/apps/details?id=neel.com.demo");
sendIntent.setType("text/plain");
startActivity(sendIntent);
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
输出
当用户长按应用程序图标时,您的快捷方式将显示如下图所示
单击快捷方式后
更新android:excludeFromRecents="true"在您的使用ShareActivity
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity
android:name=".ShareActivity"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
612 次 |
| 最近记录: |