android几个知识点笔记

时间:2022-12-28 18:31:24

--------------------android -chart---------------------

I am trying to make a simple smooth line chart which show the x axis as date-time on bottom and y


axis as normal left side. I just started using MPAndroidChart api

(https://github.com/PhilJay/MPAndroidChart) and its example app in git. In the example Line chart

I am not able to find option to change x axis labels to show on bottom instead of top as show

currently.
Just like:[sample smooth line chart]

Also is there a way to handle date-time data for x axis values ?
Please reply soon as I need to implement the chart urgently.
Also I tried my hand on androidplot api but to make the line smooth there i need to make changes

in library code which is not compiling at all in my eclipse and giving errors. Also in android

plot i did not find an option to show popup with data once i click on line chart.
So if anyone know how to make a smooth line chart using androidplot api without modifying its

library code and how to bring small popup with data in androidplot api please reply



---------------------task ----------------------
A task is a cohesive unit that can move to the "background" when users begin a new task or go to

the Home screen, via the Home button. While in the background, all the activities in the task are

stopped, but the back stack for the task remains intact—the task has simply lost focus while

another task takes place, as shown in figure 2. A task can then return to the "foreground" so

users can pick up where they left off. Suppose, for example, that the current task (Task A) has

three activities in its stack—two under the current activity. The user presses the Home button,

then starts a new application from the application launcher. When the Home screen appears, Task A

goes into the background. When the new application starts, the system starts a task for that

application (Task B) with its own stack of activities. After interacting with that application,

the user returns Home again and selects the application that originally started Task A. Now, Task

A comes to the foreground—all three activities in its stack are intact and the activity at the

top of the stack resumes. At this point, the user can also switch back to Task B by going Home

and selecting the application icon that started that task (or by selecting the app's task from

the recent apps screen). This is an example of multitasking on Android.

----------------------service----------------------
Caution: A services runs in the same process as the application in which it is declared and in

the main thread of that application, by default. So, if your service performs intensive or

blocking operations while the user interacts with an activity from the same application, the

service will slow down activity performance. To avoid impacting application performance, you

should start a new thread inside the service.
If a component starts the service by calling startService() (which results in a call to

onStartCommand()), then the service remains running until it stops itself with stopSelf() or

another component stops it by calling stopService().

If a component calls bindService() to create the service (and onStartCommand() is not called),

then the service runs only as long as the component is bound to it. Once the service is unbound

from all clients, the system destroys it.

----------------------network safe----------------------
In general, try to use the highest level of pre-existing framework implementation that can

support your use case. If you need to securely retrieve a file from a known location, a simple

HTTPS URI may be adequate and requires no knowledge of cryptography. If you need a secure tunnel,

consider using HttpsURLConnection or SSLSocket, rather than writing your own protocol.
----------------------IPC----------------------
 Android system functionality for IPC such as Intent, Binder or Messenger with a Service, and

BroadcastReceiver.

----------------------Traceview ----------------------
Traceview is a graphical viewer for execution logs saved by your application. Traceview can help

you debug your application and profile its performance.

To start Traceview, enter the following command from the SDK tools/ directory:

traceview

-----------------------ANR----------------------
单纯sleep不能使程序anr,必须是事件响应或者广播处理时间
In Android, application responsiveness is monitored by the Activity Manager and Window Manager

system services. Android will display the ANR dialog for a particular application when it detects

one of the following conditions:

    No response to an input event (such as key press or screen touch events) within 5 seconds.
    A BroadcastReceiver hasn't finished executing within 10 seconds.


------------------------Thread and Runnable----------------------------
Thread and Runnable are basic classes that, on their own, have only limited power. Instead,

they're the basis of powerful Android classes such as HandlerThread, AsyncTask, and

IntentService. Thread and Runnable are also the basis of the class ThreadPoolExecutor. This class

automatically manages threads and task queues, and can even run multiple threads in parallel.

------------------------StrictMode ----------------------------
StrictMode is a developer tool which detects things you might be doing by accident and brings

them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application's

main thread, where UI operations are received and animations take place. Keeping disk and network

operations off the main thread makes for much smoother, more responsive applications. By keeping

your application's main thread responsive, you also prevent ANR dialogs from being shown to

users.

------------------------layout-----------------------------

ViewStub is a lightweight view with no dimension and doesn’t draw anything or participate in the

layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. Each ViewStub

simply needs to include the android:layout attribute to specify the layout to inflate.

The following ViewStub is for a translucent progress bar overlay. It should be visible only when

new items are being imported into the application.

<ViewStub
    android:id="@+id/stub_import"
    android:inflatedId="@+id/panel_import"
    android:layout="@layout/progress_overlay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

Load the ViewStub Layout

When you want to load the layout specified by the ViewStub, either set it visible by calling

setVisibility(View.VISIBLE) or call inflate().

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

To avoid including such a redundant view group, you can instead use the <merge> element as the

root view for the re-usable layout. For example:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</merge>

Now, when you include this layout in another layout (using the <include/> tag), the system

ignores the <merge> element and places the two buttons directly in the layout, in place of the

<include/> tag.


Because the layout performance above slows down due to a nested LinearLayout, the performance

might improve by flattening the layout—make the layout shallow and wide, rather than narrow and

deep. A RelativeLayout as the root node allows for such layouts. So, when this design is

converted to use RelativeLayout, you can see that the layout becomes a 2-level hierarchy.

Inspection of the new layout looks like this:

Most of this time difference is due to the use of layout_weight in the LinearLayout design, which

can slow down the speed of measurement. It is just one example of how each layout has appropriate

uses and you should carefully consider whether using layout weight is necessary.


------------------------dp--------------------------------
To generate these images, you should start with your raw resource in vector format and generate

the images for each density using the following size scale:

    xhdpi: 2.0
    hdpi: 1.5
    mdpi: 1.0 (baseline)
    ldpi: 0.75

This means that if you generate a 200x200 image for xhdpi devices, you should generate the same

resource in 150x150 for hdpi, 100x100 for mdpi and finally a 75x75 image for ldpi devices.


---------------------------touch事件传递--------------------------------------------
Intercept Touch Events in a ViewGroup

The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of

a ViewGroup, including on the surface of its children. If onInterceptTouchEvent() returns true,

the MotionEvent is intercepted, meaning it will be not be passed on to the child, but rather to

the onTouchEvent() method of the parent.

The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its

children do. If you return true from onInterceptTouchEvent(), the child view that was previously

handling touch events receives an ACTION_CANCEL, and the events from that point forward are sent

to the parent's onTouchEvent() method for the usual handling. onInterceptTouchEvent() can also

return false and simply spy on events as they travel down the view hierarchy to their usual

targets, which will handle the events with their own onTouchEvent().

In the following snippet, the class MyViewGroup extends ViewGroup. MyViewGroup contains multiple

child views. If you drag your finger across a child view horizontally, the child view should no

longer get touch events, and MyViewGroup should handle touch events by scrolling its contents.

However, if you press buttons in the child view, or scroll the child view vertically, the parent

shouldn't intercept those touch events, because the child is the intended target. In those cases,

onInterceptTouchEvent() should return false, and MyViewGroup's onTouchEvent() won't be called.

public class MyViewGroup extends ViewGroup {

    private int mTouchSlop;

    ...

    ViewConfiguration vc = ViewConfiguration.get(view.getContext());
    mTouchSlop = vc.getScaledTouchSlop();

    ...

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        /*
         * This method JUST determines whether we want to intercept the motion.
         * If we return true, onTouchEvent will be called and we do the actual
         * scrolling there.
         */


        final int action = MotionEventCompat.getActionMasked(ev);

        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            // Release the scroll.
            mIsScrolling = false;
            return false; // Do not intercept touch event, let the child handle it
        }

        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                if (mIsScrolling) {
                    // We're currently scrolling, so yes, intercept the
                    // touch event!
                    return true;
                }

                // If the user has dragged her finger horizontally more than
                // the touch slop, start the scroll

                // left as an exercise for the reader
                final int xDiff = calculateDistanceX(ev);

                // Touch slop should be calculated using ViewConfiguration
                // constants.
                if (xDiff > mTouchSlop) {
                    // Start scrolling!
                    mIsScrolling = true;
                    return true;
                }
                break;
            }
            ...
        }

        // In general, we don't want to intercept touch events. They should be
        // handled by the child view.
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Here we actually handle the touch event (e.g. if the action is ACTION_MOVE,
        // scroll this container).
        // This method will only be called if the touch event was intercepted in
        // onInterceptTouchEvent
        ...
    }
}

Note that ViewGroup also provides a requestDisallowInterceptTouchEvent() method. The ViewGroup

calls this method when a child does not want the parent and its ancestors to intercept touch

events with onInterceptTouchEvent().

--------------------drawable------------------------------------
drawable-ldpi、drawable-mdpi、drawable-hdpi、drawable-xhdpi,drawable-xxhdpi,这几个目录对应的pppi



 120ppi  160ppi   240ppi    320ppi   480ppi ,也就是说如果屏幕的ppi为320,那么就会优先使用

drawable-xhdpi目录下的

图片资源。当然手机屏幕的ppi也是多种多样,例如有的是  300ppi,有的是220ppi,这个系统会选择最接近的

目录,也就

是 300ppi的手机会选择320ppi对应的drawable目录下的资源


-----------------------android:inputType--------------------------------------
android:inputType

The type of data being placed in a text field, used to help an input method decide how to let the

user enter text. The constants here correspond to those defined by InputType. Generally you can

select a single value, though some can be combined together as indicated. Setting this attribute

to anything besides none also implies that the text is editable.