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" /> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值