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" /> 
为什么我写什么 小蓝窗都没反应“PS C:\Users\Administrator> # 1. 修复错误的快捷方式 >> $desktopPath = [Environment]::GetFolderPath("Desktop") >> $badShortcut = Join-Path $desktopPath "PS7_Direct.lnk" >> >> if (Test-Path $badShortcut) { >> try { >> # 检查快捷方式目标 >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut($badShortcut) >> >> if ($shortcut.TargetPath -notlike "*pwsh.exe") { >> # 创建正确的快捷方式 >> $correctShortcut = Join-Path $desktopPath "PS7_Correct.lnk" >> $pwshPath = (Get-Command pwsh).Source >> >> $newShortcut = $shell.CreateShortcut($correctShortcut) >> $newShortcut.TargetPath = $pwshPath >> $newShortcut.Arguments = "-NoExit" >> $newShortcut.IconLocation = "$pwshPath,0" >> $newShortcut.WorkingDirectory = $desktopPath >> $newShortcut.Save() >> >> # 替换错误快捷方式 >> Remove-Item $badShortcut -Force >> Rename-Item $correctShortcut "PS7.lnk" >> >> Write-Host "已修复快捷方式: PS7.lnk" -ForegroundColor Green >> } >> } >> catch { >> Write-Host "快捷方式修复失败: $($_.Exception.Message)" -ForegroundColor Red >> } >> } >> >> # 2. 修复配置文件路径问题 >> $ps7ProfileDir = Join-Path ([Environment]::GetFolderPath('MyDocuments')) "PowerShell" >> $ps7ProfilePath = Join-Path $ps7ProfileDir "Microsoft.PowerShell_profile.ps1" >> >> $winPSProfileDir = Join-Path ([Environment]::GetFolderPath('MyDocuments')) "WindowsPowerShell" >> $winPSProfilePath = Join-Path $winPSProfileDir "Microsoft.PowerShell_profile.ps1" >> >> # 确保正确的配置文件存在 >> if (-not (Test-Path $ps7ProfilePath)) { >> # 从Windows PowerShell位置复制配置文件 >> if (Test-Path $winPSProfilePath) { >> $null = New-Item -ItemType Directory -Path $ps7ProfileDir -Force >> Copy-Item $winPSProfilePath $ps7ProfilePath -Force >> Write-Host "已复制配置文件到PowerShell 7位置" -ForegroundColor Cyan >> } >> else { >> # 重新创建配置文件 >> $profileContent = @' >> # PowerShell 7 基础配置 >> Write-Host "✅ PowerShell $($PSVersionTable.PSVersion) 已加载" -ForegroundColor Green >> Set-Alias ll Get-ChildItem >> '@ >> $profileContent | Out-File $ps7ProfilePath -Encoding UTF8 >> Write-Host "已创建基础配置文件" -ForegroundColor Cyan >> } >> } >> >> # 3. 添加版本检测和自动修复 >> $autoFixScript = @' >> # 自动环境修复脚本 >> function Repair-PS7Environment { >> # 检查PowerShell版本 >> if ($PSVersionTable.PSVersion.Major -lt 7) { >> Write-Host "⚠️ 当前运行的是PowerShell $($PSVersionTable.PSVersion)" -ForegroundColor Red >> Write-Host "正在尝试启动PowerShell 7..." -ForegroundColor Yellow >> >> try { >> # 尝试通过快捷方式启动 >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcut = Join-Path $desktop "PS7.lnk" >> >> if (Test-Path $shortcut) { >> $shell = New-Object -ComObject WScript.Shell >> $shell.Run($shortcut) >> } >> else { >> # 直接启动pwsh >> Start-Process pwsh -ArgumentList "-NoExit" >> } >> >> # 退出当前会话 >> exit >> } >> catch { >> Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red >> } >> } >> >> # 检查配置文件加载 >> if (-not (Test-Path $PROFILE)) { >> Write-Host "⚠️ 配置文件未找到" -ForegroundColor Yellow >> Write-Host "正在创建基础配置文件..." -ForegroundColor Cyan >> >> @' >> # PowerShell 7 基础配置 >> Write-Host "✅ PowerShell $($PSVersionTable.PSVersion) 已加载" -ForegroundColor Green >> Set-Alias ll Get-ChildItem >> '@ | Out-File $PROFILE -Encoding UTF8 >> } >> } >> >> # 启动时自动运行修复 >> Repair-PS7Environment >> '@ >> >> # 将修复脚本添加到配置文件 >> $currentProfile = if (Test-Path $ps7ProfilePath) { >> Get-Content $ps7ProfilePath -Raw >> } else { "" } >> >> if (-not $currentProfile.Contains("Repair-PS7Environment")) { >> $autoFixScript + "`n`n" + $currentProfile | Out-File $ps7ProfilePath -Encoding UTF8 >> Write-Host "已添加自动修复功能到配置文件" -ForegroundColor Green >> } >> >> # 4. 创建验证脚本 >> $verificationScript = @' >> # 环境验证脚本 >> Write-Host "`n=== PowerShell 7 环境验证 ===" -ForegroundColor Cyan >> >> # 1. 版本验证 >> Write-Host "PowerShell 版本: $($PSVersionTable.PSVersion)" >> if ($PSVersionTable.PSVersion.Major -ge 7) { >> Write-Host "✅ 版本验证通过" -ForegroundColor Green >> } else { >> Write-Host "❌ 需要PowerShell 7或更高版本" -ForegroundColor Red >> } >> >> # 2. 配置文件验证 >> Write-Host "`n配置文件状态: " -NoNewline >> if (Test-Path $PROFILE) { >> Write-Host "已加载" -ForegroundColor Green >> Write-Host "配置文件路径: $PROFILE" >> } else { >> Write-Host "未找到" -ForegroundColor Red >> } >> >> # 3. 快捷方式验证 >> Write-Host "`n桌面快捷方式验证:" >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcuts = Get-ChildItem $desktop -Filter "PS7*.lnk" >> >> if ($shortcuts) { >> foreach ($shortcut in $shortcuts) { >> $shell = New-Object -ComObject WScript.Shell >> $sc = $shell.CreateShortcut($shortcut.FullName) >> >> $status = if ($sc.TargetPath -like "*pwsh.exe") { >> "✅ 有效" >> } else { >> "❌ 无效" >> } >> >> Write-Host " - $($shortcut.Name): $status" -ForegroundColor $( >> if ($status -eq "✅ 有效") { "Green" } else { "Red" } >> ) >> Write-Host " 目标: $($sc.TargetPath)" >> Write-Host " 参数: $($sc.Arguments)" >> } >> } else { >> Write-Host " - 未找到PS7快捷方式" -ForegroundColor Yellow >> } >> >> # 4. 基本功能测试 >> Write-Host "`n基本功能测试:" >> try { >> $result = Invoke-RestMethod -Uri "https://httpbin.org/get" -TimeoutSec 5 >> Write-Host " - HTTP测试: 成功" -ForegroundColor Green >> } catch { >> Write-Host " - HTTP测试: 失败 - $($_.Exception.Message)" -ForegroundColor Red >> } >> >> try { >> $json = '{"test": true}' | ConvertFrom-Json >> Write-Host " - JSON测试: 成功" -ForegroundColor Green >> } catch { >> Write-Host " - JSON测试: 失败" -ForegroundColor Red >> } >> >> Write-Host "`n验证完成!" -ForegroundColor Cyan >> '@ >> >> # 保存验证脚本 >> $verifyPath = Join-Path $env:TEMP "Verify-PS7Environment.ps1" >> $verificationScript | Out-File $verifyPath -Encoding UTF8 >> >> # 5. 启动验证 >> Write-Host "`n正在启动环境验证..." -ForegroundColor Cyan >> Start-Process pwsh -ArgumentList "-NoExit", "-File `"$verifyPath`"" >> >> # 检查PowerShell版本 >> $PSVersionTable.PSVersion >> >> # 检查配置文件 >> Test-Path $PROFILE >> >> # 检查快捷方式目标 >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut("C:\Users\Administrator\Desktop\PS7.lnk") >> $shortcut.TargetPath >> >> # 直接运行验证脚本 >> & "$env:TEMP\Verify-PS7Environment.ps1" >> >> # 创建一键修复快捷方式 >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcutPath = Join-Path $desktop "Fix_PS7.lnk" >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut($shortcutPath) >> $shortcut.TargetPath = "pwsh.exe" >> $shortcut.Arguments = "-Command `"& {Start-Process pwsh -ArgumentList '-NoExit','-File',`"$verifyPath`"}`"" >> $shortcut.IconLocation = "C:\Windows\System32\shell32.dll,2" >> $shortcut.Save() >> >> # 每月自动验证环境 >> $trigger = New-JobTrigger -Daily -At "9:00 AM" -DaysInterval 30 >> $action = { >> & "$env:TEMP\Verify-PS7Environment.ps1" | Out-File "$env:TEMP\PS7_HealthCheck.log" >> } >> Register-ScheduledJob -Name "PS7_Maintenance" -ScriptBlock $action -Trigger $trigger >>”
最新发布
08-17
@echo off :: 设置网页URL和快捷方式名称 set "URL=https://www.baidu.com" set "LNK_NAME=百度首页" set "TEMP_ICO=%TEMP%\web_favicon.ico" :: 获取桌面路径 for /f "tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop 2^>nul') do set "DESKTOP=%%b" if not defined DESKTOP set "DESKTOP=%USERPROFILE%\Desktop" :: 尝试下载网页图标 echo 正在尝试获取网页图标... powershell -Command "(New-Object Net.WebClient).DownloadFile('%URL%/favicon.ico', '%TEMP_ICO%')" >nul 2>&1 :: 检查图标是否下载成功 if exist "%TEMP_ICO%" ( set "ICON_LOCATION=%TEMP_ICO%,0" ) else ( set "ICON_LOCATION=%URL%,0" echo 未找到网页图标,将使用浏览器默认图标 ) :: 创建VBS脚本函数 call :CreateShortcut "%DESKTOP%%LNK_NAME%.lnk" call :CreateShortcut "D:%LNK_NAME%.lnk" :: 清理临时文件 if exist "%TEMP_ICO%" del /f /q "%TEMP_ICO%" echo 快捷方式创建完成! echo 桌面位置: "%DESKTOP%%LNK_NAME%.lnk" echo D盘位置: "D:%LNK_NAME%.lnk" pause exit /b :CreateShortcut set "VBS_SCRIPT=%TEMP%\create_shortcut_%RANDOM%.vbs" ( echo Set oWS = WScript.CreateObject("WScript.Shell"^) echo sLinkFile = "%~1" echo Set oLink = oWS.CreateShortcut(sLinkFile^) echo oLink.TargetPath = "rundll32.exe" echo oLink.Arguments = "url.dll,FileProtocolHandler %URL%" echo oLink.WorkingDirectory = "%%windir%%" echo oLink.IconLocation = "%ICON_LOCATION%" echo oLink.Description = "网页快捷方式 - %URL%" echo oLink.Save )>"%VBS_SCRIPT%" cscript //nologo "%VBS_SCRIPT%" >nul del /f /q "%VBS_SCRIPT%" >nul 2>&1 exit /b 使用管理员权限自动执行完成
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值