Android——api阅读记录
打算完整地将android官方api文档阅读一遍,并做简要记录。
2017/08/02:
1、蓝牙。主要是BluetoothAdapter的操作,与tcp的socket serversocket类似。
2、AysncTaskHandler and AysnctaskLoader:用于异步加载使用,特别是Asynctaskloader会比较实用,可以用在fragment或者activity的异步加载中。
3、在标题栏中可以直接添加searchview,代码如下:
Item = menu.add("Search");
item.setIcon(androi.R.draw.Search);
item.setView(new Searchview)
4、关于BroadCasTreceiver,发送本地广播,使用localbroadmanager.seentbroadcast();因为广播的onreceive()方法会立刻执行并返回,如果有耗时的操作要做,可以在onreceive()方法中调用Pendingintent result = goAysnc(),子异步线程中操作,执行完毕后,调用result.finish()
5、获取手机已安装应用的图标及名字的代码如下:
List<ApplicationInfo> apps = mPm.getInstalledApplications( PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_DISABLED_COMPONENTS);还可以注册监听应用安装或者卸载的广播,还有一点,可以在自定义广播的构造方法中直接进行注册,这样就省去了在外部调用后需要注册的麻烦,提升封装力度。
public static class PackageIntentReceiver extends BroadcastReceiver { final AppListLoader mLoader; public PackageIntentReceiver(AppListLoader loader) { mLoader = loader; IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addDataScheme("package"); mLoader.getContext().registerReceiver(this, filter); // Register for events related to sdcard installation. IntentFilter sdFilter = new IntentFilter(); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); mLoader.getContext().registerReceiver(this, sdFilter); } @Override public void onReceive(Context context, Intent intent) { // Tell the loader about the change. mLoader.onContentChanged(); }}6、关于剪贴板,使用clipboardmanager,可以剪切string,uri,还有intent.
7、contentProvider使用:
实现contentprovider可实现getType()用于返回类型,如果存储的是文件、图片、视频,那么contentprovider存储的应该是这些文件对应的路径,这个时候如果需要返回类型,那么就需要实现getStreamType(Uri , filtertype),可以返回一个数组,如{image/jpg,image/png}
provider的权限设置:在manifest的provider组建声明中,添加android:permission 或者 android:readPermission 或者android:writePermission 设置相应的属性,这样如果别的程序希望访问这个provider,那么就需要在manifest添加该权限声明。还有一种临时权限的说法:在manifest的provider的声明中,添加android:grantPermission属性,用途:假设您需要实现电子邮件提供程序和应用的权限,如果您想允许外部图像查看器应用显示您的提供程序中的照片附件, 为了在不请求权限的情况下为图像查看器提供必要的访问权限,可以为照片的内容 URI 设置临时权限。 对您的电子邮件应用进行相应设计,使应用能够在用户想要显示照片时向图像查看器发送一个 Intent,其中包含照片的内容 URI 以及权限标志。 图像查看器可随后查询您的电子邮件提供程序以检索照片,即使查看器不具备对您提供程序的正常读取权限,也不受影响。用法:要向应用授予临时访问权限,Intent 必须包含 FLAG_GRANT_READ_URI_PERMISSION
和/或 FLAG_GRANT_WRITE_URI_PERMISSION
标志。 它们通过 setFlags()
方法进行设置。
数据库的批量操作,applyBatch(List
8、Context:deleteDataBase() deleteFile() deleteSharepreference()删除这个应用的某个文件或者数据库
9、intent使用:
action+data配对使用如下,可进行特殊操作:
-
ACTION_VIEW
content://contacts/people/1 -- Display information about the person whose identifier is "1". -
ACTION_DIAL
content://contacts/people/1 -- Display the phone dialer with the person filled in. -
ACTION_VIEW
tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what is considered the most reasonable thing for a particular URI. -
ACTION_DIAL
tel:123 -- Display the phone dialer with the given number filled in. -
ACTION_EDIT
content://contacts/people/1 -- Edit information about the person whose identifier is "1". -
ACTION_VIEW
content://contacts/people/ -- -
10、SearchView的使用,当用户在输入框输入时,就跟在百度里面输入一样,会自动联想,有相应的自动补全和推荐功能。可自动联想之前的输入历史,或者自定义联想内容。可参考文档实现。
-
11、ActivityInfo 对应AndroidManifest中的
标签,所以manifest中的activity的属性值及含义,在ActivityInfo都能找到;同理,ApplicationInfo对应的是标签;PackageInfo对应的是整个AndroidManifest,包括versionCode versionNam,permission,shareUserId,所有的activities services receivers. -
12、labelIntent:封装了intent,包含string和icon,可以很方便地用于发送CHOOSE的action.
2017-08-21
1、PackageManager:可以获取有关activity application的图标,权限等等,可以认为是android系统所有app的管家,想查询某个app的相关信息,就可以通过packagemanager来访问。有一个帖子介绍packagemanager非常详细的:http://blog.csdn.net/lusing/article/details/52229795
有相关方法比较重要的:
queryBroadCastReceiver(Intent, flags);queryContentProviders(intent,flags),queryIntentActivities(intent,flags);queryIntentServices(Intent intent, int flags);这些方法会返回一个列表,里面包含了能处理参数中的intent的组件;还有如下方法可以返回单个的组件:
resolveActivity(Intent intent, int flags);resolveContentProvider(String name, int flags);resolveService(Intent intent, int flags)
方法类型2:
void setComponentEnabledSetting (ComponentName componentName, int newState,int flags)这个方法能激活或者关闭某个组件。
常量3:
包含了androidmanifest中所有的feature(其实是一个过滤器,如果指定了我们的app需要某些feature,那么在那些没有我们指定需要的手机上面就不会显示我们的app).
动态权限检测:int checkPermission (String permName, String pkgName)
检测签名:int checkSignatures (String pkg1, String pkg2)
2、PackageStates:里面的常量包含了该应用的cachesize,codesize,mediasize等等关于存储容量的使用的数据。
3、PathPermission:用来表示Providerinfo类中的pathpermission的信息,providerInfo = PackageManager.resolveContentProvider()
.
4、permissiongroup,就是permission里面的一个tag。主要作用是用来显示application需要的权限,比如你使用了
5、ShortCutManager:该功能是android7.0推出的新功能,效果描述如下:
普通应用,在桌面上点击图标时进入到我们的activity中,使用shortcutmanager,当用户在桌面上长按应用图标时,会弹出一个shortcuts的列表(具体数量自己可以设置),可以通过点击item直接进入到想进入的界面。譬如,message应用,如果我希望能给小明快速发送短信,那么我就可以添加一个shortcuts,当我长按message图标时,可以直接进入到给小明发短信的界面。一篇讲得比较好的帖子:http://blog.csdn.net/qibin0506/article/details/52878690
android.content.res:
1、关于打开文件,或者assets文件夹的相关信息:
ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open("ss", ParcelFileDescriptor.MODE_READ_WRITE); AssetFileDescriptor assetFileDescriptor = new AssetFileDescriptor(parcelFileDescriptor,0,parcelFileDescriptor.getStatSize());
2、Resource:几乎涵盖了所有有关于资源的操作,包括获取string,text,boolean,dimesnions,R.layout.xxx,R.animation.xxx,R.id.assets资源(音乐,图片等等没有被appt工具处理的资源),获取屏幕分辨率, 设置,还有quantitystring(某些国家单复数的表示不一样)并同时进行格式化。
3、AssetManager:getLocale()方法可以获取当前国家;主要方法是打开文件。
4、ColorStateList:对应于xml文件中的selector(当用于空间颜色时,可以根据控件的不同状态显示不同的颜色);从api 23开始还能设置这些颜色的透明度,方法withAlpha(int alpha)
5、Configuration:能够通过这个获取很多系统信息:国家,字体,分辨率,输入法,输入键盘是否隐藏,屏幕方向,layout方向。
6、TypeArray:在自定义方法中的,根据obtainStyledAttribute()可以获取到typearray,然后用typearray就可以获取一些资源值,dimension,color,string,int等等。
更多相关文章
- ActivityManagerService启动流程详解
- android中使用代码适配屏幕,dp与px互转、sp与px互转
- Mac OS X下搭建Android(安卓)Source编译环境的问题及解决方法
- Xposed Android(安卓)hook -方法介绍
- Android(安卓)利用程序实现GPS的打开或关闭
- Android消息机制(Handler、Looper、MessageQueue)
- Android(安卓)QuickSetting---手电筒控制
- Android:inflate.inflate()方法
- Android(安卓)UI编程(1)——九宫格(GridView)