2014-01-23 148 views
0

我想在安装时在home screen上创建应用程序的快捷方式。安装时无法在主屏幕上创建应用程序的快捷方式安卓android

我的AndroidManifest.xml

<activity 
     android:name="com.example.test.ShortCutActivity" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/title_activity_short_cut" > 

     <intent-filter> 
      <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

</activity> 

将此添加到我也有我的项目,ShortCutActivity名称创建一个Actvity。

ShortCutActivity.java

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

    ShortcutIconResource icon = 
      Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); 

     Intent intent = new Intent(); 

     Intent launchIntent = new Intent(this,MainActivity.class); 

     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut"); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 

     setResult(RESULT_OK, intent); 
} 

但它不产生任何快捷方式。我错过了什么吗?

回答

2

使用您的活动此示例代码,在AndroidManifest.xml中

private void createShortcut(){ 
    Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); 
    shortcutIntent.setAction(Intent.ACTION_MAIN); 
    Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo"); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    getApplicationContext().sendBroadcast(intent); 
} 

也作为一个发射器和主要适用于您的AndroidManifest.xml以下权限

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

希望这将为你工作

+0

这将只在我启动应用程序时起作用,我希望创建快捷方式当安装应用程序时 –

+0

实际上这个选项在谷歌市场的开发人员配置文件中提供,设置选项 –

+0

我们只能在第一次启动活动后以编程方式创建快捷方式 –

-1
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
shortcutIntent.setClassName(this, this.getClass().getName()); 

Intent intent = new Intent();  
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");  
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 
intent.setAction(Intent.ACTION_CREATE_SHORTCUT); 

getApplicationContext().sendBroadcast(intent); 
相关问题