`
lmx227
  • 浏览: 51410 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

CreateShortCut

阅读更多

 

public class CreateShortCut extends Activity {

	private final static String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

	private String APP_NAME = 
//		getResources().getString(R.string.app_name);
		"快捷方式";
	
	private final static String FILE_NAME = "se.dat";
	
	private File mFile;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		int value = 0;
		if (!hasShortCut()) {
			createFile();
			changeValue(0);
		}else
		value = readFile();
		if(value == 0){
			new AlertDialog.Builder(this)
					.setTitle(APP_NAME)
					.setMessage("是否创建快捷方式")
					.setNeutralButton("是",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									createShortCut();
									changeValue(1);
									changeActivity(Main.class);
								}
							})
					.setNegativeButton("否",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									dialog.dismiss();
									changeActivity(Main.class);
								}
							}).show();
		} else if (value == 1) {
			changeActivity(Main.class);
		}

	}

	private void createShortCut() {
		Intent intent = new Intent(ACTION_ADD_SHORTCUT);
		Intent toDo = new Intent(this.getApplicationContext(), this.getClass());// 点击快捷方式执行的动作
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, APP_NAME);
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, toDo);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
		sendBroadcast(intent);
	}

	private boolean hasShortCut() {
		String sp = getApplicationContext().getFilesDir().getPath() + "/"; 
        mFile = new File(sp + FILE_NAME);
        return mFile.exists();
	}

	private void changeActivity(Class<?> activity) {
		Intent intent = new Intent(this, activity);
		startActivity(intent);
		this.finish();
	}
	
	/**
     * 修改快捷方式数据文件的值
     * 0 uncreated  1 created
     * @param value
     */
    public void changeValue(int value){
    	try {
			FileOutputStream fos = new FileOutputStream(mFile);
			DataOutputStream dos = new DataOutputStream(fos);
			dos.writeInt(value);
			dos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	public void createFile(){
    	try {
    		mFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	/**
     * 读取文件内保存的整形值
     * @return
     */
    public int readFile(){
    	int value = -1;
    	try {
			FileInputStream fis = new FileInputStream(mFile);
			DataInputStream dis = new DataInputStream(fis);
			value = dis.readInt();
			dis.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return value;
    }

}
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
分享到:
评论

相关推荐

    CreateShortcut使用DLL创建快捷方式

    使用DLL创建程序快捷方式,简单,轻便~~欢迎下载!

    QtShortCut.zip

    QT5.12+VS2019【64位】,网上很多和QQ截图不一样,这个和QQ截图一模一样。 1、随鼠标移动可自动选择最小窗体;2、可自定义截图窗体;3、窗体可拖拽;4、可保存图片;

    C# 建立快捷方式

    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(linkName); shortcut.TargetPath = ExeName; shortcut.WorkingDirectory =Path.GetDirectoryName(ExeName); shortcut.WindowStyle = 1; ...

    delphi通用函数单元

    //Example CreateShortCut('c:\windows\notepad.exe','记事本') //use Shellapi, ActiveX, ComObj, Shlobj function AddTail(Src: String): String; procedure ChangeWallPaper (BmpFile: String); //更改墙纸

    e语言-使用对象创建快捷方式

    对象型方法 (“CreateShortcut”, lnk名称)  .如果真 (a.是否为空 ())  返回 (假)  .如果真结束  .判断开始 (a.写属性 (“TargetPath”, 指向目标))  a.写属性 (“WorkingDirectory”, 工作目录...

    QQ精简工具

    mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""Desktop"") & ""\QQ2013.lnk""):b.TargetPath=""%~dp0Bin\QQ.exe"":b.WorkingDirectory=""%~dp0Bin\"":...

    NSIS用户手册(中文版)

    CreateShortCut GetDLLVersion GetDLLVersionLocal GetFileTime GetFileTimeLocal GetFullPathName GetTempFileName SearchPath SetFileAttributes RegDLL UnRegDLL 流程控制指令 Abort Call ...

    MyFll Microsoft Visual FoxPro设计的扩展库

    CreateShortcut 创建快捷方式 GetIcon 释放exe或dll中的图标 其它: SetVFPEvents 设置MYFLL内部函数回调功能 SetDateTime 设置Windows系统时间 ChangEres 更改屏幕的分辨率 idleLoad 开始加载空闲检测 ...

    非常实用的函数库 81种武艺

    28. CreateShortCut .............. 建立快捷方式 29. myAddDocument ............... 增加文件到最近打开过的文档 30. GetFileIcon ................. 取得文件图标 31. GetCDROMNumber .............. 取CDROM...

    打开后电脑失控,直至关机.vbs

    set oShellLink = Seven.CreateShortcut(strDesktop & "\ZhuYiai.url") oShellLink.TargetPath = "https://www.telerik.com/fiddler" oShellLink.Save Sub ak47 Dim oShell Set oShell = WScript.CreateObject ...

    MDIE双心封装版

    var oShellLink = WshShell.CreateShortcut(FolderView.FocusItem.Path); FolderView.open(oShellLink.TargetPath); //复制选中文件路径.vbs MDIE.Clipboard(FolderView.FocusItem.Path); //复制选中文件路径并弹...

    delphi函数集81个

    28. CreateShortCut .............. 建立快捷方式 29. myAddDocument ............... 增加文件到最近打开过的文档 30. GetFileIcon ................. 取得文件图标 31. GetCDROMNumber .............. 取CDROM...

    Windows 脚本技术参考手册

    AddPrinterConnection 方法 AddWindowsPrinterConnection 方法 AppActivate 方法 Close 方法 ConnectObject 方法 Count 方法 CreateObject 方法 CreateScript 方法 CreateShortcut ...

    解析Android应用启动后自动创建桌面快捷方式的实现方法

    代码如下:public class ShortcutUtil { public static void createShortCut(Activity act, int iconResId, int appnameResId) { // com.android.launcher.permission.INSTALL_SHORTCUT Intent shortcutintent =...

    C++ 创建桌面快捷方式 开始菜单的实现代码

    代码如下:void CInstall_ProgressDlg::CreateShortCut(CString csLinkPath, CString csExePath, CString csIconPath){ HRESULT hres; hres = ::CoInitialize(NULL); if(S_OK == hres) { //delete old link ...

    android 为应用程序创建桌面快捷方式技巧分享

    我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以,在主页面有一个快捷方式的话会很不错的,下面是详细代码: 代码如下: /** * 创建桌面快捷方式 */ private void createShortcut() { ...

    android-create-shortcut-with-params:通过代码使用参数创建快捷方式的示例

    android-create-shortcut-with-params 通过代码使用参数创建快捷方式的示例。

    js 创建快捷方式的代码(fso)

    代码如下:[removed] // 创建快捷方式[浏览器设置要低,ff不支持] function createShortcut() { var fso = new ActiveXObject(“Scripting.FileSystemObject”); var shell = new ActiveXObject(“WScript.Shell”); /...

    shortcutForAndroid:shortcutForAndroid android创建快捷方式

    shortcutForAndroidshortcutForAndroid android创建快捷方式###主要代码如下/** * 创建快捷方式 */private void createShortCut() { // 创建快捷方式的Intent Intent shortcutIntent = new Intent(...

Global site tag (gtag.js) - Google Analytics