- setResult(RESULT_OK, intent);
有时我们会看到有些应用一安装完就在桌面添加了一个shortcut或是程序中可以点击一下按钮就在桌面创建一个shortcut,这时我们要用到Launcher中的一个广播接收者,在Launcher的manifest文件中:
- <receiver
- android:name="com.android.launcher2.InstallShortcutReceiver"
- android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
- <intent-filter>
- <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
- </intent-filter>
- </receiver>
下面通过一个例子来说明下用法:通过点击一个按钮在桌面创建一个shortcut。
TestCreateShortcut.java
- public class TestCreateShortcut extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test_shortcut);
- Button button = (Button) findViewById(R.id.install);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- //install_shortcut action
- intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
- //点击shortcut时进入的activity,这里是自己
- intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
- TestCreateShortcut.this, TestCreateShortcut.class));
- //shortcut的name
- intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyShortcut");
- Parcelable iconResource = Intent.ShortcutIconResource
- .fromContext(TestCreateShortcut.this, R.drawable.icon);
- //shortcut的icon
- intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- iconResource);
- //是否可以重复放置shortcut,默认true
- intent.putExtra("duplicate", false);
- sendBroadcast(intent);
- }
- });
- }
- }
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
