android 快捷方式开发(二)桌面添加快捷方式

来源:互联网 发布:c语言新建工程步骤 编辑:程序博客网 时间:2024/04/28 04:23

效果:在桌面上长按在弹出框中选择快捷方式->应用程序->将添加快捷方式的程序

1:在第一个界面的onCreate方法中添加

startActivity(new Intent(ShortCutsActivity.this,
                    InstallMenuShortcut.class));

2:创建InstallMenuShortcut

package com.wop.ShortCuts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

public class InstallMenuShortcut extends Activity {
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 要添加的快捷方式Intent
        Intent addShortcut = new Intent();
        mContext = this;
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext
                .getResources().getString(R.string.app_name));
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
                R.drawable.ic_launcher);//ic_launcher快捷方式的icon
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        Intent ootStartIntent = new Intent(mContext, ShortCutsActivity.class);//ShortCutsActivity 快捷方式要启动程序的第一个Activity
        ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, ootStartIntent);
        setResult(RESULT_OK, addShortcut);
        finish();
    }

3: Manifest 添加:

        <activity
            android:label="@string/app_name"
            android:name=".InstallMenuShortcut" >
            <intent-filter >
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>



原创粉丝点击