Android 创建快捷方式

快捷方式提供一种快速访问目标应用的方式。在Android开发中,同样可以从代码上创建和删除快捷方式。下面做简要说明如何处理。先看效果图『创建-创建效果-删除-删除效果』:

 添加删除快捷方式:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AActivity extends Activity implements OnClickListener {

	private Button addButton;
	private Button deleteButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addButton = (Button) findViewById(R.id.button1);
		addButton.setOnClickListener(this);

		deleteButton = (Button) findViewById(R.id.button2);
		deleteButton.setOnClickListener(this);

	}

	private void addShortcut() {
		
		// 安装的Intent
		Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		
		// 快捷名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
		shortcut.putExtra("duplicate", false);
		
		// 启动对象
		ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName());
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		
		// 快捷图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
		
		// 发送广播
		sendBroadcast(shortcut);
		
	}

	private void deleteShortcut() {
		
		// 卸载的Intent
		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
		
		// 快捷名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
		
		// 指定对象
		String appClass = this.getPackageName() + "." + this.getLocalClassName();
		ComponentName comp = new ComponentName(this.getPackageName(), appClass);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		
		// 发送广播
		sendBroadcast(shortcut);
		
	}

	@Override
	public void onClick(View v) {

		if (v == addButton) {
			addShortcut();
			return;
		}

		if (v == deleteButton) {
			deleteShortcut();
			return;
		}

	}

}
 说明:
  • 构建ComponentName时,注意“.”的使用,否则会找不到需要启动的目标;
  • 布局文件比较简单,可以在附件中找到;
  • 如果希望可以重复创建,只要设置shortcut.putExtra("duplicate", true);就OK了;

Manifest文件权限设置:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
  另一个效果:

出现在这里的方法是在Manifest文件设定的,具体如下:
        <activity-alias
            android:label="Shortcut"
            android:name=".CreateShortcuts"
            android:targetActivity=".BActivity" >
            <intent-filter >
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity-alias>
 说明:关于具体说明,还是看开发者网站吧,那里比较详细!『偷懒了』:)   这种方式实现的方式还是和上一种类似,只不过把快捷方式选择添加到了系统快捷方式页面罢了!还是需要在targetActivity中做一些操作才能创建快捷方式。下面看看BActivity做了什么吧:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

public class BActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = getIntent();
		String action = intent.getAction();
		if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			shortcut();
			finish();
			return;
		}
		
		setContentView(R.layout.activity_b);
        
    }
    
	void shortcut() {
		
		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		shortcutIntent.setClass(this, this.getClass());
		
		Intent intent = new Intent();
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷图标");
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		Parcelable shortIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortIcon);
		setResult(RESULT_OK, intent);
		
	}
    
}
 看到了吧,这里还是在代码中创建快捷方式的!:)   多说一句:一些问题的解决,与其钻牛角尖,不如借鉴源码来的更快!:)    

猜你喜欢

转载自wangleyiang.iteye.com/blog/1769597