2015-06-14 130 views
4

我想给我的用户一个选项,以创建应用程序内特定页面的快捷方式。 我在Whatsapp上看到类似的用法,当你长时间按下聊天,并且你能够创建一个桌面快捷方式到这个特定的聊天。在主屏幕上创建特定活动的快捷方式

我试过找到一些关于这个functionality的文档,但无法让它工作。 这是我有:

活动这不是发射活动(包括意向过滤器)

<activity android:name="com.my.example.pages.Topics" 
    android:parentActivityName="com.my.example.pages.Apps"> 
     <intent-filter> 
      <action android:name="android.intent.action.CREATE_SHORTCUT"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 

createShortcut功能

public void createShortcut(){ 
     Intent shortcutIntent = new Intent("com.my.example.pages.Topics"); 
     Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getActivity(), R.drawable.app_logo); 

     // The result we are passing back from this activity 
     Intent intent = new Intent(); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test"); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 
     getActivity().setResult(getActivity().RESULT_OK, intent); 
     getActivity().finish(); 

     Toast.makeText(getActivity(),"Shortcut created",Toast.LENGTH_SHORT).show(); 
    } 

清单

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

我可能因为调用函数后得到Toasts,但没有创建快捷方式,并且由于finish()方法而退出应用程序。

为了更清晰 - 如何为非启动器活动创建快捷方式?

*我在其中一个viewpager片段中运行代码。

+0

相关:http://stackoverflow.com/questions/6988511/how-to-add-apps-shortcut-to-the-home-screen – cygery

+0

与所有的答复,WhatsApp应用程序非常敬重好,顺利。作为用户,我觉得这个选项是非常有用的,甚至是必要的 – Juvi

+0

似乎这是行得通的,“主题”必须是启动器活动 –

回答

4

使用此命令为非启动器活动创建快捷方式。

private void createShortcutOfApp() { 

     Intent shortcutIntent = new Intent(getApplicationContext(), 
      YourTargetActivity.class); 
     shortcutIntent.setAction(Intent.ACTION_MAIN); 

     Intent addIntent = new Intent(); 
     addIntent 
      .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "App shortcut name"); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
     Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
     R.mipmap.logo_of_your_app_shortcut)); 

     addIntent 
      .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
     addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate 
     getApplicationContext().sendBroadcast(addIntent); 
    } 

不添加权限在menifest

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

现在定义

android:exported=”true” 

属性在

<activity> tag 

<activity 
    android:name=".YourTargetActivity" 
    android:exported="true"></activity> 

这个工程就像whatsapp app chat shorcut。

+0

@juvi请检查以上答案 –

相关问题