Android Training 学习随记

汪驰
2023-12-01

【本文是知识点的集合,格式较乱,每个知识点以" - "符号作为开头】

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) 

 
 

  1. private void share(String content, Uri uri){  
  2.     Intent shareIntent = new Intent(Intent.ACTION_SEND);   
  3.     if(uri!=null){  
  4.         shareIntent.putExtra(Intent.EXTRA_STREAM, uri);  
  5.         shareIntent.setType("image/*");   
  6.         //当用户选择短信时使用sms_body取得文字  
  7.         shareIntent.putExtra("sms_body", content);  
  8.     }else{  
  9.         shareIntent.setType("text/plain");   
  10.     }  
  11.     shareIntent.putExtra(Intent.EXTRA_TEXT, content);  
  12.     //自定义选择框的标题  
  13.     //startActivity(Intent.createChooser(shareIntent, "邀请好友"));  
  14.     //系统默认标题  
  15.     startActivity(shareIntent);  
  16. }  

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())...
  1. <shape>  
  2.             <!-- 实心 -->  
  3.             <solid android:color="#ff9d77"/>  
  4.             <!-- 渐变 -->  
  5.             <gradient  
  6.                 android:startColor="#ff8c00"  
  7.                 android:endColor="#FFFFFF"  
  8.                 android:angle="270" />  
  9.             <!-- 描边 -->  
  10.             <stroke  
  11.                 android:width="2dp"  
  12.                 android:color="#dcdcdc" />  
  13.             <!-- 圆角 -->  
  14.             <corners  
  15.                 android:radius="2dp" />  
  16.             <padding  
  17.                 android:left="10dp"  
  18.                 android:top="10dp"  
  19.                 android:right="10dp"  
  20.                 android:bottom="10dp" />  
  21.         </shape>  


-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"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_search"
          android:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="android.widget.SearchView" />
</menu>



-you can declare the logical parent of each activity by specifying theandroid:parentActivityName attribute in the <activity> 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

 类似资料: