AnyCut | 创建桌面快捷方式 | PackageManager获得系统基本信息 | UserTask多线程

http://blog.csdn.net/stefzeus/archive/2011/05/04/6393472.aspx

一、概述

AnyCut可以创建 联系人电话快捷方式、联系人短信快捷方式、系统基本Activity的快捷方式、用户自定义快捷方式。

[效果图]

    

二、获得系统基本信息

PackageManager

Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager() .

queryIntentActivities (Intent intent, int flags)
Retrieve all activities that can be performed for the given intent.

ResolveInfo

Information that is returned from resolving an intent against an IntentFilter. This partially corresponds to information collected from the AndroidManifest.xml's <intent> tags.

queryIntentActivities获得Acitivities返回了 ResolveInfo类型的List集合。 ResolveInfo其实就是解析Itent返回的信息,也是 AndroidManifest.xml 标签的信息。

 

  1. PackageManager mPackageManager;  
  2. mPackageManager = getPackageManager();  
  3. List<ResolveInfo> list = mPackageManager.queryIntentActivities(queryIntent, 0);  

 

三、创建桌面快捷方式

当点击效果图1的[Acitivity]菜单时,会进入一个列表界面,陈列了系统中所有基本的Activity子项目。选择其一后,就会将选中项目作为快捷方式显示在桌面。

设置快捷方式的名字、图标、Intent。

想必,若要在桌面创建其他文件的快捷方式,关键也是通过设置Intent 来实现。:)

 

  1. @Override  
  2. protected void onListItemClick(ListView list, View view, int position, long id) {  
  3.     ResolveInfoWrapper wrapper = (ResolveInfoWrapper) getListAdapter().getItem(position);  
  4.     ResolveInfo info = wrapper.getInfo();  
  5.     // Build the intent for the chosen activity  
  6.     Intent intent = new Intent();  
  7.     intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,  
  8.             info.activityInfo.name));  
  9.     Intent result = new Intent();  
  10.     result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  11.     // Set the name of the activity  
  12.     result.putExtra(Intent.EXTRA_SHORTCUT_NAME, info.loadLabel(mPackageManager));  
  13.     // Build the icon info for the activity  
  14.     Drawable drawable = info.loadIcon(mPackageManager);  
  15.     if (drawable instanceof BitmapDrawable) {  
  16.         BitmapDrawable bd = (BitmapDrawable) drawable;  
  17.         result.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());  
  18.     }  
  19.     // Set the result  
  20.     setResult(RESULT_OK, result);  
  21.     finish();  
  22. }  

 

四、UserTask多线程

Android在更新界面时,必须要在UI线程中进行。利用UserTask可以解决耗时操作导致ANR (界面更新慢)的问题。

扩展UserTask类:

1.在doInBackground做耗时操作,此处是用来获得系统所以基本Activity的项目;

2.在onPreExecute做初始化操作(界面初始化),此处是显示List滚动条;

3.onPostExecute用作耗时操作完成后要执行的工作(更新界面),此处时更新ListActivity的内容显示。

 

  1.     @Override  
  2.     protected void onCreate(Bundle savedState) {  
  3.         ...  
  4.         // Start loading the data  
  5.         new LoadingTask().execute((Object[]) null);  
  6.     }  
  7. private final class LoadingTask extends UserTask<Object, Object, ActivityAdapter> {  
  8.         @Override  
  9.         public void onPreExecute() {  
  10.             setProgressBarIndeterminateVisibility(true);  
  11.         }  
  12.         @Override  
  13.         public ActivityAdapter doInBackground(Object... params) {  
  14.             // Load the activities  
  15.             Intent queryIntent = new Intent(Intent.ACTION_MAIN);  
  16.             List<ResolveInfo> list = mPackageManager.queryIntentActivities(queryIntent, 0);  
  17.             // Sort the list  
  18.             Collections.sort(list, new ResolveInfo.DisplayNameComparator(mPackageManager));  
  19.             // Make the wrappers  
  20.             ArrayList<ResolveInfoWrapper> activities = new ArrayList<ResolveInfoWrapper>(list.size());  
  21.             for(ResolveInfo item : list) {  
  22.                 activities.add(new ResolveInfoWrapper(item));  
  23.             }  
  24.             return new ActivityAdapter(ActivityPickerActivity.this, activities);  
  25.         }  
  26.         @Override  
  27.         public void onPostExecute(ActivityAdapter result) {  
  28.             setProgressBarIndeterminateVisibility(false);  
  29.             setListAdapter(result);  
  30.         }  
  31.     }  

 

五、其他技巧

1. extends ListActivity

当一个Activity只显示一个ListView时,可以让它继承ListActivity,而不是继承Activity.

2. String集合

 

  1.    <string-array name="mainMenu">  
  2.        <item>Direct call</item>  
  3.        <item>Direct text message</item>  
  4.        <item>Activity</item>  
  5.        <item>Make your own</item>  
  6.    </string-array>  
  7.         
  8. setListAdapter(ArrayAdapter.createFromResource(this, R.array.mainMenu,  
  9.                android.R.layout.simple_list_item_1));  

 

3.List排序

Collections.sort (list, new ResolveInfo.DisplayNameComparator(mPackageManager));

[参考]

ResolveInfo

http://www.moandroid.com/?p=1095

获取应用基本信息

http://hiapk.com/thread-23708-1-1.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值