CreateShortCut

3965 ワード

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" />