热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

创建联系人、短信的桌面快捷方式

看了一个开源的一个小项目,是制作联系人电话、短信还有应用的桌面快捷方式的,先将效果图贴上:publicclassCreateShortCu

看了一个开源的一个小项目,是制作联系人电话、短信还有应用的桌面快捷方式的,先将效果图贴上:

                                   

public class CreateShortCutActivity extends ListActivity implements DialogInterface.OnClickListener,DialogInterface.OnCancelListener{private static final int REQUEST_PHONE = 1;private static final int REQUEST_TEXT = 2;private static final int REQUEST_ACTIVITY = 3;private static final int REQUEST_CUSTOM = 4;private static final int LIST_ITEM_DIRECT_CALL = 0;private static final int LIST_ITEM_DIRECT_TEXT = 1;private static final int LIST_ITEM_ACTIVITY = 2;private static final int LIST_ITEM_CUSTOM = 3;private static final int DIALOG_SHORTCUT_EDITOR = 1;private Intent mEditorIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setListAdapter(ArrayAdapter.createFromResource(this, R.array.mainMenu, android.R.layout.simple_list_item_1));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {switch (position) {case LIST_ITEM_DIRECT_CALL:{Intent intent = new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI);intent.putExtra(Contacts.Intents.UI.TITLE_EXTRA_KEY, R.string.callShortcutActivityTitle);startActivityForResult(intent, REQUEST_PHONE);break;}case LIST_ITEM_DIRECT_TEXT:{Intent intent = new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI);intent.putExtra(Contacts.Intents.UI.TITLE_EXTRA_KEY,getText(R.string.textShortcutActivityTitle));startActivityForResult(intent, REQUEST_TEXT);break;}case LIST_ITEM_ACTIVITY:break;case LIST_ITEM_CUSTOM:break;default:break;}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode != RESULT_OK) {return;}switch (requestCode) {case REQUEST_PHONE:startShortcutEditor(generateShortcutIntent(data,R.drawable.sym_action_call, "tel", Intent.ACTION_CALL));break;case REQUEST_TEXT: {startShortcutEditor(generateShortcutIntent(data,R.drawable.sym_action_sms, "smsto", Intent.ACTION_SENDTO));break;}case REQUEST_ACTIVITY:break;case REQUEST_CUSTOM:break;default:break;}}private void startShortcutEditor(Intent shortcutIntent) {mEditorIntent = shortcutIntent;showDialog(DIALOG_SHORTCUT_EDITOR);}/*** Returns an Intent describing a direct text message shortcut* @param data The data from the phone number picker* @param actionResId* @param scheme* @param action* @return An Intent describing a phone number shortcut*/private Intent generateShortcutIntent(Intent data, int actionResId,String scheme, String action) {Uri phoneUri = data.getData();long personId = 0;String name = null;String number = null;int type;Cursor cursor = getContentResolver().query(phoneUri, new String[]{Phones.PERSON_ID, Phones.NAME, Phones.NUMBER, Phones.TYPE},null, null, null);try {cursor.moveToFirst();personId = cursor.getLong(0);name = cursor.getString(1);number = cursor.getString(2);type = cursor.getInt(3);}finally{if (null != cursor) {cursor.close();}}Intent intent = new Intent();Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatePhoneNumberIcon(personUri, type, actionResId));//Make the URI a direct call ,so that it will always continue to work.phoneUri = Uri.fromParts(scheme, number, null);intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action, phoneUri));intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);return intent;}/*** Generates a phone number shortcut icon.Adds an overlay describing the type of the phone number ,* add if there is a photo also adds the call action icon* @param personUri The person the phone number belongs to* @param type The type of the phone number* @param actionResId The ID fot the action resource* @return The bitmap for the icon*/private Bitmap generatePhoneNumberIcon(Uri personUri, int type,int actionResId) {final Resources resources = getResources();boolean drawPhoneOverlay = true;Bitmap photo = People.loadContactPhoto(this, personUri, 0, null);if (null == photo) {//If there is not a photo use the generic phone action icon insteadBitmap phoneIcon = getPhoneActionIcon(resources, actionResId);if(null != phoneIcon){photo = phoneIcon;drawPhoneOverlay = false;}else{return null;}}//Set up the drawing classint iconSize = (int)resources.getDimension(android.R.dimen.app_icon_size);Bitmap icon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);Canvas canvas = new Canvas(icon);//Copy in the photoPaint photoPaint = new Paint();photoPaint.setDither(true);photoPaint.setFilterBitmap(true);Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());Rect dst = new Rect(0, 0, iconSize, iconSize);canvas.drawBitmap(photo, src, dst, photoPaint);// Create an overlay for the phone number typeString overlay = null;switch (type) {case Phones.TYPE_HOME:overlay = "H";break;case Phones.TYPE_MOBILE:overlay = "M";break;case Phones.TYPE_WORK:overlay = "W";break;case Phones.TYPE_PAGER:overlay = "P";break;case Phones.TYPE_OTHER:overlay = "O";break;}if (overlay != null) {Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);textPaint.setTextSize(20.0f);textPaint.setTypeface(Typeface.DEFAULT_BOLD);textPaint.setColor(resources.getColor(R.color.textColorIconOverlay));textPaint.setShadowLayer(3f, 1, 1, resources.getColor(R.color.textColorIconOverlayShadow));canvas.drawText(overlay, 2, 16, textPaint);}return icon;}/*** Return the phone action icon* @param resources The resource to load the icon from* @param actionResId The resource id to load* @return The icon for the phone action icon*/private Bitmap getPhoneActionIcon(Resources resources, int resId) {Drawable phoneIcon = resources.getDrawable(resId);if (phoneIcon instanceof BitmapDrawable) {BitmapDrawable bitmapDrawable = (BitmapDrawable)phoneIcon;return bitmapDrawable.getBitmap();}else {return null;}}@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case DIALOG_SHORTCUT_EDITOR:return new ShortcutEditorDialog(this, this, this);default:break;}return super.onCreateDialog(id);}@Overrideprotected void onPrepareDialog(int id, Dialog dialog) {switch (id) {case DIALOG_SHORTCUT_EDITOR:if (null != mEditorIntent) {ShortcutEditorDialog editorDialog = (ShortcutEditorDialog)dialog;editorDialog.setIntent(mEditorIntent);mEditorIntent = null;}break;default:break;}}@Overridepublic void onClick(DialogInterface dialog, int which) {if (which == DialogInterface.BUTTON1) {ShortcutEditorDialog editorDialog = (ShortcutEditorDialog)dialog;Intent intent = editorDialog.getIntent();installShortcut(intent);}removeDialog(DIALOG_SHORTCUT_EDITOR);}private void installShortcut(Intent intent) {//Broadcast an intent that tells the home screen to create a new shortcut intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");sendBroadcast(intent);// Inform the user that the shortcut has been createdToast.makeText(this, R.string.shortcutCreated, Toast.LENGTH_SHORT).show();}public void onCancel(DialogInterface dialog) {// Remove the dialog, it won't be used againremoveDialog(DIALOG_SHORTCUT_EDITOR);}
}

public class ShortcutEditorDialog extends AlertDialog implements OnClickListener, TextWatcher{private static final String STATE_INTENT = "intent";private boolean mCreated = false;private Intent mIntent;private ImageView mIconView;private EditText mNameView;private OnClickListener mClickListener;private OnCancelListener mCancelListener;protected ShortcutEditorDialog(Context context, OnClickListener clickListener,OnCancelListener cancelListener) {super(context);mClickListener = clickListener;mCancelListener = cancelListener;View view = getLayoutInflater().inflate(R.layout.shortcut_editor, null, false);setTitle(R.string.shortcutEditorTitle);setButton(context.getString(android.R.string.ok), this);setButton2(context.getText(android.R.string.cancel), mClickListener);setOnCancelListener(mCancelListener);setCancelable(true);setView(view);mIconView = (ImageView)view.findViewById(R.id.shortcutIcon);mNameView = (EditText)view.findViewById(R.id.shortcutName);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);if(null != savedInstanceState){mIntent = savedInstanceState.getParcelable(STATE_INTENT);}mCreated = true;if (null != mIntent) {loadIntent(mIntent);}}private void loadIntent(Intent intent) {// Show the iconBitmap iconBitmap = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);if (null != iconBitmap) {mIconView.setImageBitmap(iconBitmap);} else {ShortcutIconResource shortIconResource = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);if (null != shortIconResource) {int res = getContext().getResources().getIdentifier(shortIconResource.resourceName, null,shortIconResource.packageName);mIconView.setImageResource(res);} else {mIconView.setVisibility(View.INVISIBLE);}}// Fill in the name field for editingmNameView.addTextChangedListener(this);mNameView.setText(intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));// Ensure the intent has the proper flagsintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);}@Overridepublic void onClick(DialogInterface dialog, int which) {if (which == BUTTON1) {String name = mNameView.getText().toString();if (null == name || (null != name && name.length() ==0)) {mNameView.setError(getContext().getText(R.string.errorEmptyName));return;}mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);}mClickListener.onClick(dialog, which);}public void setIntent(Intent intent) {mIntent = intent;if (mCreated) {loadIntent(mIntent);}}public Intent getIntent() {mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mNameView.getText().toString());return mIntent;}@Overridepublic void afterTextChanged(Editable text) {if (text.length() == 0) {mNameView.setError(getContext().getText(R.string.errorEmptyName));}elsemNameView.setError(null);}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}}程序的序列图如下:

转载:http://www.moandroid.com/?p=1699


在应用程序中添加快捷图标

Launcher为了让其他应用程序能够定制自己的快捷图标,就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用sendBroadcast方法就可以创建一个快捷图标了。那么,要构造怎样一个Intent才会被Launcher的BroadcastReceiver接收呢?我们还是先来看看这个BroadcastReceiver的注册信息吧。
下面是Launcher的AndroidManifest.xml文件中Install-ShortcutReceiver的注册信息。


android:name=”.InstallShortcutReceiver”
android:permission= “com.android.launcher.permission.INSTALL_SHORTCUT”>
<intent-filter>

intent-filter
>

如何向这个 BroadcastReceiver 发送广播&#xff0c;设置如下&#xff1a;


  1.首先应用程序必须要有com.android.launcher.permission.INSTALL_SHORTCUT权限&#xff1b;


2..然后广播出去的Intent的action设置com.android.launcher.action.INSTALL_SHORTCUT&#xff1b;


  3.这样广播就可以发送给Launcher的InstallShortcutReceiver了&#xff1b;

而快捷图标的信息则是以附加信息的形式存储在广播出去的Intent对象中的&#xff0c;包括有图标、显示的名称以及用来启动目标组件的Intent这三种信息。我们可以通过putExtra的重载方法&#xff0c;通过指定相应的键值&#xff0c;将这些信息放到附加信息的Bundle对象中。

列出了各种快捷图标信息相对应的键值和数据类型&#xff1a;

shortcut

下面举些具体的例子&#xff0c;如下&#xff1a;

private final String ACTION_ADD_SHORTCUT &#61;
“com.android.launcher.action.INSTALL_SHORTCUT”;
Intent addShortcut &#61;new Intent(ACTION_ADD_SHORTCUT);
String numToDial &#61; null;
Parcelable icon &#61; null;

numToDial &#61; “110″;
icon &#61; Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

//numToDial &#61; “119″;
//icon &#61; Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);

//图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);

//名称
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);

//启动目标组件的Intent
Intent directCall;
directCall.setData(Uri.parse(“tel://”&#43;numToDial));
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,directCall);
sendBroadcast(addShortcut);
上面的程序运行后的界面如下&#xff1a;
110-119

只要知道这些信息后&#xff0c;你就可以轻而易举的为应用程序添加快捷图标。





推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • switch语句的一些用法及注意事项
    本文介绍了使用switch语句时的一些用法和注意事项,包括如何实现"fall through"、default语句的作用、在case语句中定义变量时可能出现的问题以及解决方法。同时也提到了C#严格控制switch分支不允许贯穿的规定。通过本文的介绍,读者可以更好地理解和使用switch语句。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Java SE从入门到放弃(三)的逻辑运算符详解
    本文详细介绍了Java SE中的逻辑运算符,包括逻辑运算符的操作和运算结果,以及与运算符的不同之处。通过代码演示,展示了逻辑运算符的使用方法和注意事项。文章以Java SE从入门到放弃(三)为背景,对逻辑运算符进行了深入的解析。 ... [详细]
author-avatar
赤血-魔鬼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有