Shortcut 快捷方式 使用 - 1

来源:互联网 发布:知否 齐衡 编辑:程序博客网 时间:2024/05/18 15:56

[原理] 
1. 系统有一个BroadcastReceiver 其action = "com.android.launcher.action.INSTALL_SHORTCUT" 用于接收与快捷方式有关的Intent 
2. 我们要做的就是:把快捷方式的有关信息装入对应的Intent 然后发送出去即可 

[代码] 
1. 定义对应的Intent 

Java代码 
  1. private final String ACTION_ADD_SHORTCUT =  
  2.         "com.android.launcher.action.INSTALL_SHORTCUT";  
  3. Intent intent = new Intent(ACTION_ADD_SHORTCUT);   


2. 把有关信息装入Intent 比如 图标 名字 其所引起的Intent 
Java代码 
  1. Intent dial = new Intent(Intent.ACTION_CALL);  
  2.         dial.setData(Uri.parse("tel://110"));  
  3.         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Dial to 110");  
  4.         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,dial);  
  5.         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));  
  6.           

(当按下该快捷方式后 就会拨打110) 

3. 发送之 
Java代码 
  1. sendBroadcast(intent);  

原创粉丝点击