android shortcut 权限,Android之ShortCut[Broadcast方式]

packagecom.zhy.shortcut;importandroid.app.Activity;importandroid.content.ComponentName;importandroid.content.Intent;importandroid.content.SharedPreferences;importandroid.content.SharedPreferences.Editor;importandroid.os.Bundle;importandroid.preference.PreferenceManager;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassShortCutActivityextendsActivity {privatestaticfinalString CREATE_SHORTCUT_ACTION="com.android.launcher.action.INSTALL_SHORTCUT";privatestaticfinalString DROP_SHORTCUT_ACTION="com.android.launcher.action.UNINSTALL_SHORTCUT";privatestaticfinalString PREFERENCE_KEY_SHORTCUT_EXISTS="IsShortCutExists";

Button button;//获取默认的SharedPreferencesSharedPreferences sharedPreferences ;//从SharedPreferences获取是否存在快捷方式 若不存在返回false 程序第一次进来肯定返回falsebooleanexists ;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);

exists=sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS,false);//创建桌面快捷方式//若第一次启动则创建,下次启动则不创建if(!exists) {

setUpShortCut();

}

setContentView(R.layout.main);

button=(Button) findViewById(R.id.dropShortCut);

button.setOnClickListener(newOnClickListener() {

@OverridepublicvoidonClick(View v) {

tearDownShortCut();

}

});

}/*** 创建桌面快捷方式*/privatevoidsetUpShortCut() {

Intent intent=newIntent(CREATE_SHORTCUT_ACTION);//设置快捷方式图片intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.logo));//设置快捷方式名称intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"sina");//设置是否允许重复创建快捷方式 false表示不允许intent.putExtra("duplicate",false);//设置快捷方式要打开的intent//第一种方法创建快捷方式要打开的目标intentIntent targetIntent=newIntent();//设置应用程序卸载时同时也删除桌面快捷方式targetIntent.setAction(Intent.ACTION_MAIN);

targetIntent.addCategory("android.intent.category.LAUNCHER");

ComponentName componentName=newComponentName(getPackageName(),this.getClass().getName());

targetIntent.setComponent(componentName);//第二种方法创建快捷方式要打开的目标intent/** Intent

* targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName

* ());*/intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);//发送广播sendBroadcast(intent);

Editor editor=sharedPreferences.edit();

editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS,true);

editor.commit();

}/*** 删除桌面快捷方式*/privatevoidtearDownShortCut() {

Intent intent=newIntent(DROP_SHORTCUT_ACTION);//指定要删除的shortcut名称intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"sina");

String appClass=getPackageName()+"."+this.getLocalClassName();

ComponentName component=newComponentName(getPackageName(), appClass);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent().setAction(Intent.ACTION_MAIN).setComponent(component));

sendBroadcast(intent);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值