【本文是知识点的集合,格式较乱,每个知识点以" - "符号作为开头】
training文档:http://developer.android.com/training/index.html
-通过implicit intent设置具体的Intent.ACTION来打开其他app时,要记得先判断是否有对应的activity能打开,否则会crash:
两种方法,第一种详细查询,第二种比较简单:
PackageManager packageManager = getPackageManager()
;
List activities = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
——————————————————————————————————————————————————————————————————
if (intent.resolveActivity(getPackageManager()) != null)
If a feature is not a core part of your app's functionality, you should consider handing the work over to another app
-判断网络是否通畅:
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())...
-Use android:animateLayoutChanges="true" to animate when the layout changes. 使用animateLayoutChanges来控制layout变动时产生动画效果。
your app should not add a Back button to the UI.
When launching another application's activity to allow the user to say, compose an email or pick a photo attachment, you generally don't want the user to return to this activity if they relaunch your application from the Launcher (the device home screen). It would be confusing if touching your application icon brought the user to a "compose email" screen.当启动其他app的activity时候,比如说要写邮件或者添加图片附件,通常都不希望用户从launcher中重新启动你的app的时候,仍然回到这个第三方app的activity。如果点击你的图标回到了编写邮件界面,用户可能会产生困惑。
To prevent this from occurring, simply add the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
flag to the intent used to launch the external activity, like so:为了防止这种事情,只要在lauch第三方activity的时候,在intent加一个flag就行:
Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
externalActivityIntent.setType("image/*");
externalActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(externalActivityIntent);
-添加searchView的方法:
To add a SearchView
widget to the app bar, create a file named res/menu/options_menu.xml
in your project and add the following code to the file
xml version="1.0" encoding="utf-8"?>
-you can declare the logical parent of each activity by specifying theandroid:parentActivityName
attribute in the
element.
通过在manifest文件中,指定activity的"android: parentActivityName"属性来指定其逻辑上的父Activity。(以实现actionbar的向上操作。)
AlarmManager让你可以在app生命周期之外,执行基于时间的操作。比如用alarm来初始化一个长期运行的操作,例如每天开启一次service下载天气预报。对于能确定『during』the life time of your application的timing operations。使用Handler和Time,Thread结合,这会让android系统更好地控制系统资源。
Generally speaking, avoid creating short-term temporary objects if you can. Fewer objects created mean less-frequent garbage collection, which has a direct impact on user experience. 如有可能,不要创建短期对象。对象越少,意味着GC的频率越低。
If you don't need to access an object's fields, make your method static. Invocations will be about 15%-20% faster.
如果不需要访问对象的成员,把方法写成static。调用速度会提高20%
It is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.
在接口中,使用getter和setter调用是很常见的面向对象的习惯。但是在类的内部,永远不要使用get方法,而是要直接访问变量。
activities should do as little as possible to set up in key life-cycle methods such as onCreate() andonResume()
activity应该在关键的生命周期方法中(比如onCreate或者onResume),做尽可能少的工作
The most effective way to create a worker thread for longer operations is with the AsyncTask class.
最有效的,创建一个worker thread来处理长时间操作的方法是,写一个asynctask类。
Although it's more complicated than AsyncTask, you might want to instead create your own Thread orHandlerThread class. If you do, you should set the thread priority to "background" priority by callingProcess.setThreadPriority() and passing THREAD_PRIORITY_BACKGROUND. If you don't set the thread to a lower priority this way, then the thread could still slow down your app because it operates at the same priority as the UI thread by default.
虽然比AsyncTask 复杂,你可能会想自己写thread或者handlerThread,这样的话,你得设置线程的优先级。使用Process.setThreadPriority()方法,参数传THREAD_PRIORITY_BACKGROUND.如果不设置一个较低的优先级,那么这个thread依然能拖慢你app的速度,因为默认是跟UI线程一个优先级的。
【关于内存泄露的文章】
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1225/3800.html