"Android first line code" note

Source: Internet
Author: User
Tags control label

I've been learning Android for almost two years and have done a lot of projects. Recently took the leisure time wants to strengthen the Android Foundation, previously saw on the net several Guo Lin Guo Dashin's several blogs, benefited a lot. It took nearly a week to finish reading a Guo Shen Android textbook, the first line of Android code. This book is more basic than other teaching materials, and the content is very practical. After reading I also have some harvest, learned some can be very good use of the actual Android tips, below to choose what I think is very valuable place to make a record. At the same time welcome you to add ~ ~

1. See which activity the current interface is in.

Many times when we take over a more complex source code, there will be a lot of acvitity. At this point, it is often difficult to find out which interface corresponds to which source code, which will undoubtedly reduce our development efficiency. Here's a little tip from this book:
1. Write a new activity such as baseactivity to inherit activity, the code is as follows:

public  class  BaseActivity extends Activity{           @Override           protectedvoidonCreate(Bundle savedInstanceState)           {                        super.onCreate(savedInstanceState);                        Log.d("BaseActivity", getClass().getSimpleName());           }}
    1. Let all the activity in the project inherit from Baseactivity;
    2. Click into the interface where you want to see the code, while observing the output in the Logcat, and the printed information is the corresponding activity's class name.
personal experience: see here, perhaps you will feel, if you get a source project, and then go in one of the changes will be very troublesome. I think the author is here mainly to introduce such a method, we can consider adding such a skill when programming, so that we can better work with people a project. 2. Avoid parameter confusion when starting an activity that needs to pass data.

Everyone is familiar with intent, he is the porter of the Android system. When we start a acivity and want to pass some data to it, we usually use intent.
There may be a lot of activity in a project that needs to pass data, and the developer is completely unsure what data the activity is going to pass, and we can use a technique to standardize such a "communication protocol."

publicclass AnotherActivity extends BaseActivity{        publicstaticvoidactionStart(Context context,String val1,String val2)        {                  new Intent(context, AnotherActivity.class);                  intent.putExtra("val1", val1);                  intent.putExtra("val2", val2);                  context.startActivity(intent);        }}

In this way, in the process of use, when you need to jump to use the following code

AnotherActivity.actionStart(FirstActivity.this"val1""val2");
Personal experience: Personally think this is a very good habit, if the system with the parameters of the jump more should be converted to all the unified specification, this can reduce the cooperation in the development of unnecessary trouble. 3, the use of tablelayout

Tablelayout is a layout that is seldom used in development. Of course there are a few places for this control that I think I need to be aware of.
-the control in the TableRow label (a row of tablelayout) cannot be specified in width.
-Because TableRow cannot set the width when the layout auto-stretch width is required, you can set it by the Tablelayout tab

android:stretchColunms="1"

property to specify that a column is stretched to resolve (the above is the specified stretch of the second column).
-When a row is less than one column of data, it will end up with a column of blanks. If you want the system to automatically stretch the space that fills this line, you can set it under a control label for the current row:

android:layout_span="2"

This allows the control to occupy 2 positions.

personal experience: In the projects I have done, Tablelayout has not been used, but as an Android developer, it should have a certain understanding, in the right time can be very good to use. 4. Customize the adapter that inherits Arrayadapter

We use the adapter when using the ListView, Android has made two commonly used adapters for us: Simpleadapter and arrayadapter etc., when we have special requirements, we need to customize an adapter to inherit from Baseadapter, This makes it more flexible to perform the special functions we need. But most of the time we can write an adapter that inherits from Arrayadapter, which is easier:

public   Class  mcadapter   extends   arrayadapter  <fruit  > { private  int  resourceId; public  Fruitadapter (context context, int  Textviewresourceid, list<fruit> objec TS) {super  (context, Textviewresourceid, objects); ResourceId = Textviewresourceid; } @Override public  View getView (int  position, Vi EW Convertview, ViewGroup parent) {... return  view; }

In this way, we only need to rewrite the GetView method, and if it is inherited from Baseadapter, we need to rewrite at least 4 methods. Can be more simplified for our code.

Personal Experience: Maybe this everybody knows, anyway, I used to be a custom adapter inherits from Baseadapter and then overrides 4 methods, 3 of which are not used. 5, Fragment Rewind stack

Here is just a way to record a fragment: Transaction.addtobackstack (null);

6. Broadcast Receiver
    • Open thread not allowed in broadcast receiver class
    • Send ordered broadcasts: Sendorderedbroadcast
    • To register an ordered broadcast priority in a broadcast receiver:
              <intent-filter **android:priority="50">              ......              <intent/>      <receive/>
    • Block broadcast: Abortbroadcast ();
    • Local broadcast: Localbroadcastmanager.sendbroadcast (intent); (cannot use static registration mode)
Personal experience: Radio receivers are very common. The priority of the ordered broadcast by the application of their own to set this point I am very surprised, it is not to give the application vulnerability can be drilled? In previous applications to intercept text messages were achieved by setting a higher broadcast priority and then blocking the broadcast, but after Android4.4, the default SMS management feature was added, which enabled the user to decide who would manage the SMS, phone book, and so on. It seems that Google has also found this potential problem. 7. Call System Camera

In the process of using the system camera, we can start the camera via Startactivityforresult to receive the return value of the success of the photo. And you can crop the picture after the photo is taken.
-Start camera Photo:

Intent intent = new intent("android.media.action.IMAGE_CAPTURE");intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, TAKE_PHOTO);
    • Crop after the photo is finished:
Intent intent = new intent("android.media.action.CROP");intent.setDataAndType"image/*");intent.putExtra("scale", true);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, CROP_PHOTO);
Personal experience: Here is the understanding of the action in the system and the familiarity with the action's parameters. See Android Action Encyclopedia

Well, these are some of the things I've learned in this book, and the little tricks that I'm sitting on to offer can help us develop our projects and improve our efficiency if they are used properly. I will continue to join the practical skills I have learned in the future. You are also welcome to correct and supplement the content. Thank you!

                                                                                                                 ——超低空

"Android first line code" note

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.