Android development-implementation code for automatic shutdown

Source: Internet
Author: User

Any development language is difficult, so do not feel bored and patient in the course of learning. Let's talk about how to add custom controls for Android widgets. First, let's look at a reference:

ARemoteViews object (and, consequently, an App Widget) can support thefollowing layout classes:

* FrameLayout

* LinearLayout

* RelativeLayout

Andthe following widget classes:

* AnalogClock

* Button

* Chronometer

* ImageButton

* ImageView

* ProgressBar

* TextView

Descendantsof these classes are not supported.

It can be seen that only seven widgets can be used in our widgets: AnalogClock, Button, Chronometer, ImageButton, mageView, ProgressBar, TextView, Listview, Editview, scrollview and other frequently-used controls cannot be used in our widgets. In fact, the source code of all these controls is stored in the framework/base/core/java/android/widget directory. The reason why these seven controls are available is that the @ RemoteView label is added, let's take a look at the source code:

AnalogClock. java: 39: @ RemoteView

Java-40-publicclass AnalogClock extends View {

ImageButton. java: 66: @ RemoteView

ImageButton. java-67-publicclass ImageButton extends ImageView {

.....

Therefore, if you want to use controls such as Listview in widgets, you need to write the same class as Listview by yourself and add the @ RemoteView tag, and copy it to the directory framework/base/core/java/android/widget.

Then we can use the control we wrote in the Widget. Because it has the @ RemoteView tag like other available controls, it can be recognized by the RemoteView object.

Now that we know the principle, you can follow the steps below to implement it (taking my custom AnalogClock as an example ):

1. first, I fully copied the AnalogClock in the source code. java is named MyClock. java to the framework/base/core/java/android/widget directory, and then modify the code as needed.

2. The resource file used by MyClock. java must be stored in the frameworks/base/core/res directory. It must be referenced in this way: com. android. internal. R. drawable .*

However, changing resources in this way is not convenient. We know that each system control has a style file, so my approach is:

First look at the style source file of the system AnalogClock. java:

.....

Frameworks/base/core/res/values/attrs. xml:

Therefore, if you want to use controls such as Listview in widgets, you need to write the same class as Listview by yourself and add the @ RemoteView tag, and copy it to the directory framework/base/core/java/android/widget.

Then we can use the control we wrote in the Widget. Because it has the @ RemoteView tag like other available controls, it can be recognized by the RemoteView object.

Now that we know the principle, you can follow the steps below to implement it (taking my custom AnalogClock as an example ):

1. first, I fully copied the AnalogClock in the source code. java is named MyClock. java to the framework/base/core/java/android/widget directory, and then modify the code as needed.

2. The resource file used by MyClock. java must be stored in the frameworks/base/core/res directory. It must be referenced in this way: com. android. internal. R. drawable .*

However, changing resources in this way is not convenient. We know that each system control has a style file, so my approach is:

First look at the style source file of the system AnalogClock. java:

Frameworks/base/core/res/values/attrs. xml:

Private Drawable mMinuteHand;

Public MyClock (Context context, AttributeSet attrs, intdefStyle ){

Super (context, attrs, defStyle );

Resources r = context. getResources ();

TypedArray a = context. obtainStyledAttributes (attrs, com. android. internal. R. styleable. AnalogClock, defStyle, 0 );

MMinuteHand = a. getDrawable (com. android. internal. R. styleable. AnalogClock_hand_minute); // call the parameters in attrs. xml.

If (mMinuteHand = null ){

MMinuteHand = r. getDrawable (com. android. internal. R. drawable. clock_hand_minute); // The resource in the frameworks/base/core/res directory of The Calling System

}

}

I define it in the widget layout configuration file:

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: id = "@ + id/rl_widget_clockView"

Android: hand_minute = "@ drawable/minute_white" // reference resources under local drawable

Android: layout_width = "80dp"

Android: layout_height = "80dp"

/>

.. I also tested the source code repeatedly.

3. At this time, most of the work has been completed. We need to compile the entire SDK. However, note that I encountered various compilation errors during the compilation process, and the prompts are all unknown, which is very confusing. In the end, I summed up a set of compilation methods with a high success rate.

First compile the entire pure source code in the root directory make-j4 (Be careful not to add any classes and resources we define ourselves), after about 1-2 hours of successful compilation, copy our custom classes and resources to the framework and compile it again in the root directory make-j4.

To test the code we added, run the emulator command to start a new simulator after the compilation is successful, and then install our APK on it.

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.