1. check whether there is any network connection
JavaCode
1. Public static Boolean isnetworkavailable (context ){
2. connectivitymanager CM = (connectivitymanager) Context. getsystemservice (
3. Context. connectivity_service );
4. networkinfo info = cm. getactivenetworkinfo ();
5. Return (info! = NULL & info. isconnected ());
6 .}
Public static Boolean isnetworkavailable (context ){
Connectivitymanager CM = (connectivitymanager) Context. getsystemservice (
Context. connectivity_service );
Networkinfo info = cm. getactivenetworkinfo ();
Return (info! = NULL & info. isconnected ());
}
2. Full Screen and untitled
Java code
1. // full screen (no statusbar)
2. getwindow (). setflags (windowmanager. layoutparams. Flag _ fullscreen,
3. windowmanager. layoutparams. Flag _ fullscreen );
4. // No title bar (no titlebar)
5. requestwindowfeature (window. Feature _ No _ title );
// Full screen (no statusbar)
Getwindow (). setflags (windowmanager. layoutparams. Flag _ fullscreen,
Windowmanager. layoutparams. Flag _ fullscreen );
// No title bar (no titlebar)
Requestwindowfeature (window. Feature _ No _ title );
3. Use viewstub to expand the view with latency
Java code
1) define viewstub
2. <viewstub Android: Id = "@ + ID/stub _ import"
3. Android: inflatedid = "@ + ID/panel _ import"
4. Android: Layout = "@ layout/progress _ overlay"
5. Android: Layout _ width = "fill _ parent"
6. Android: layout_height = "wrap_content"
7. Android: layout_gravity = "bottom"/>
8. 2) Expand the view
9. findviewbyid (R. Id. stub_import). setvisibility (view. Visible );
10. // or get the view
11. View importpanel = (viewstub)
12. findviewbyid (R. Id. stub_import). Inflate ();
1) define viewstub
<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"/>
2) Expand the view
Findviewbyid (R. Id. stub_import). setvisibility (view. Visible );
// Or obtain the view
View importpanel = (viewstub)
Findviewbyid (R. Id. stub_import). Inflate ();
4. Delete the window background
1) coding implementation
Java code
1. Public void oncreate (bundle icicle ){
2. Super. oncreate (icicle );
3. setcontentview (R. layout. mainview );
4. // Delete the window background
5. getwindow (). setbackgrounddrawable (null );
6 ....
7 .}
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. mainview );
// Delete the window background
Getwindow (). setbackgrounddrawable (null );
...
}
2) custom theme implementation
Java code
1. <resources>
2. <MCE: style name = "nobackgroundtheme" parent = "Android: Theme"> <! --
3. <item name = "Android: windowbackground"> @ null </item>
4. --> </MCE: style> <style name = "nobackgroundtheme" parent = "Android: Theme" mce_bogus = "1"> <item name = "Android: windowbackground "> @ null </item> </style>
5. </resources>
<Resources>
<MCE: style name = "nobackgroundtheme" parent = "Android: Theme"> <! --
<Item name = "Android: windowbackground"> @ null </item>
--> </MCE: style> <style name = "nobackgroundtheme" parent = "Android: Theme" mce_bogus = "1"> <item name = "Android: windowbackground "> @ null </item> </style>
</Resources>
6. Do not reload oncreate () during screen Switching ()
To prevent the activity from reloading oncreate when switching between landscape and landscape screens,
You only need to add attributes to the activity configuration file:
Android: configchanges = "orientation | keyboardhidden | navigation"
7. translucent theme
1. Resource source file location: Res/values/styles. xml
<Style name = "mytranslucenttheme" parent = "Android: style/theme. translucent">
<Item name = "Android: windowbackground"> @ drawable/BG </item>
</Style>
BG is the index of a translucent image.
2. Use custom translucent theme
Androidmanifest. xml
Java code
1. <activity Android: Name = ". wallpaperactivity"
2. Android: Label = "@ string/wallpaper_titile"
3. Android: theme = "@ style/mytranslucenttheme">
4. </activity>
<Activity Android: Name = ". wallpaperactivity"
Android: Label = "@ string/wallpaper_titile"
Android: theme = "@ style/mytranslucenttheme">
</Activity>
8. Obtain thumbnails of images and videos on Android phones.
We all know that the system will call the mediascanner Service to perform background scanning and index new songs, images, videos, and other information when the SD card is inserted in Android 1.5, if you need to quickly extract images and video thumbnails, you can directly access android. provider. mediastore. images. thumbnails and Android. provider. mediastore. video. thumbnails databases can be used to query thumbnails.
How to determine files? You can use cursor to traverse the database and compare the value of the internal_content_uri field. This is a URI that can be converted to a string. Here, the complete path of the multimedia file on the Android mobile phone SD card is saved,
For specific thumbnails, you can use getthumbnail (contentresolver Cr, long origid, int kind, bitmapfactory. options options) or getthumbnail (contentresolver Cr, long origid, long groupid, int kind, bitmapfactory. options options). The two methods return the bitmap type, while the thumbnail resolution can be extracted from the height and width fields. on Android, the thumbnails are divided into two types.
The kind fields are obtained by using the micro_kind and mini-kind modes, respectively. The former has lower resolution. In this way, we can directly call the system thumbnails when obtaining an image preview of the file system, instead of re-computing it by ourselves.
Finally, android123 prompts you to save the thumbnails in the dcim directory of the SD card. thumbnails is an image, while. video_thumbnails is a video. These two folders are hidden attributes and can be seen in common file managers.
9. Android ANR introduction and Avoidance
Many new Android Developers may find ANR. What is Android ANR? In fact, ANR is the full name of application not responding. When an application remains suspended for a long time, the android system will pop up a window that says, XXX is not responding provides two buttons: force close and wait.
Possible ANR triggering
1. Long I/O processing, such as reading and writing large files and blocking caused by network access.
2. for time-consuming operations, the default value of Android is more than ms, that is, the ANR window is displayed in 5 seconds. Some applications may not cache for the first time, you can use splash to play the flash screen logo and other methods to delay loading.
3. Pay attention to the use of multithreading in service and appwidget unless it and activity work in different processes.
How to Avoid ANR
1. Open a worker thread separately and process time-consuming content through independent threads or asynctask-like methods.
2. Perform time-consuming operations in segments as much as possible. A method similar to a state machine is used. Activity objects like Symbian perform complex tasks in segments.
3. in the UI thread, do not process too much content. For example, if a 5 MB text is sent to textview to settext, there is no good solution for this UI operation, so android123 prompts, when you need to perform complex operations in the UI, you can refer to the segment processing method mentioned in above 2.
10. Comparison between asynctask and thread and handler
Many netizens may find that many apps on the Android platform use asynctask instead of thread and handler to update the UI. Here, android123 tells you the difference between them, which solution should we use at ordinary times. The system has introduced asynctask to Android since Android 1.5. in the OS package, in the past, when the 1.1 and 1.0 sdks officially named it usertask, it was a newly added concurrent library starting with JDK 1.5, users who have worked on J2EE may understand the efficiency and power of the concurrent library, which is more flexible and powerful than the original Java thread.
It occupies more system resources. Thread was designed to implement multithreading in Java in the early days. It is relatively simple and does not support many features of concurrent which need to be implemented by themselves in synchronization and thread pool classes, for distributed applications, you need to write scheduling code by yourself. For the refreshing of Android UI, Google introduces the handler and logoff mechanisms, which are implemented based on messages, the message queue may be blocked or cannot be used accurately due to other reasons.
Android Development Network recommends that you use asynctask instead of thread + handler, which is easier to call and more reliable after actual tests, google uses a large number of asynchronous tasks in browser as time-consuming I/O operations, such as downloading files and reading and writing databases. They are essentially inseparable from messages, however, asynctask is more reliable and easier to maintain than thread-based handler. However, asynctask has some disadvantages. For example, once the thread is enabled, the dobackground method cannot send messages to the thread, the logic can only be controlled through pre-configured tags. Of course, the logic can be communicated by changing the pending flag bit of the thread. For some applications, thread, handler, and
Logoff may be more flexible.
11. How to Use the Android. OS. debug debugging tool
In Android software development, the final performance testing and debugging appear to be important. Today, android123 will discuss with you the Android app that Google gave to us. OS. debug class, which is relatively simple to use. We first import android. OS. debug this package.
Add Debug. startmethodtracing ("/sdcard/cwj") Where tracing needs to be started ");
Add Debug. stopmethodtracing () to the end where debugging is stopped ();
We can use traceview In SDK ools to view the final cwj file generated on sdcard.
12. textview multi-line scrolling in this article is easy to implement
In Android, We can embed a textview in scrollview to implement text scrolling. In fact, textview can also implement multi-row scrolling. After all, scrollview must have only one direct subclass layout. You can easily set several attributes in layout.
Java code
1. <textview
2. Android: Id = "@ + ID/tvcwj"
3. Android: layout_width = "fill_parent"
4. Android: layout_height = "wrap_content"
5. Android: scrollbars = "vertical" <! -- Vertical scroll bar -->
6. Android: singleline = "false" <! -- Implement multiple rows -->
7. Android: maxlines = "15" <! -- No more than 15 rows -->
8. Android: textcolor = "# ff0000"
9./>
<Textview
Android: Id = "@ + ID/tvcwj"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: scrollbars = "vertical" <! -- Vertical scroll bar -->
Android: singleline = "false" <! -- Implement multiple rows -->
Android: maxlines = "15" <! -- No more than 15 rows -->
Android: textcolor = "# ff0000"
/>
Of course, we also need to use the setmovementmethod method of textview to set a rolling instance to make textview move. The Code is as follows:
Java code
1. textview tvandroid123 = (textview) findviewbyid (R. Id. tvcwj );
2. tvandroid123.setmovementmethod (scrollingmovementmethod. getinstance (); // you can view the Android. Text. Method branch in the SDK to learn more.
Textview tvandroid123 = (textview) findviewbyid (R. Id. tvcwj );
Tvandroid123.setmovementmethod (scrollingmovementmethod. getinstance (); // you can view the Android. Text. Method branch in the SDK to learn more.
12. simple widget creation process
1. Define an intent in onupdate ()
Intent startactivityintent = new intent (context, widgetlife. Class );
2. instantiate a pendingintent
Pendingintent pintent = pendingintent. getactivity (context, 0, startactivityintent, 0 );
3. instance remoteview
Remoteviews activityview = new remoteviews (context. getpackagename (), R. layout. widget_layout );
4. Button event
Activityview. setonclickpendingintent (R. Id. Button, pintent );
5. Update Widgets
Appwidgetmanager. updateappwidget (appwidgetids, activityview );
12. Dialog Box [Dialog] removes the white border code
Border, dialog, Android, dialog box, code
Create a styles. xml file in the values directory and write the following code:
Java code
1. <resources>
2. <MCE: style name = "dialog" parent = "@ Android: style/theme. Dialog"> <! --
3. <item name = "Android: windowframe"> @ null </item>
4. <item name = "Android: kerberwisfloating"> true </item>
5. <item name = "Android: javaswistranslucent"> false </item>
6. <item name = "Android: windownotitle"> true </item>
7. <item name = "Android: background"> @ Android: color/Black </item>
8. <item name = "Android: windowbackground"> @ null </item>
9. <item name = "Android: backgrounddimenabled"> false </item>
10.
11. --> </MCE: style> <style name = "dialog" parent = "@ Android: style/theme. dialog "mce_bogus =" 1 "> <item name =" Android: windowframe "> @ null </item>
12. <item name = "Android: kerberwisfloating"> true </item>
13. <item name = "Android: javaswistranslucent"> false </item>
14. <item name = "Android: windownotitle"> true </item>
15. <item name = "Android: background"> @ Android: color/Black </item>
16. <item name = "Android: windowbackground"> @ null </item>
17. <item name = "Android: backgrounddimenabled"> false </item>
18. </style>
19. </resources>
<Resources>
<MCE: style name = "dialog" parent = "@ Android: style/theme. Dialog"> <! --
<Item name = "Android: windowframe"> @ null </item>
<Item name = "Android: incluwisfloating"> true </item>
<Item name = "Android: javaswistranslucent"> false </item>
<Item name = "Android: windownotitle"> true </item>
<Item name = "Android: background"> @ Android: color/Black </item>
<Item name = "Android: windowbackground"> @ null </item>
<Item name = "Android: backgrounddimenabled"> false </item>
--> </MCE: style> <style name = "dialog" parent = "@ Android: style/theme. dialog "mce_bogus =" 1 "> <item name =" Android: windowframe "> @ null </item>
<Item name = "Android: incluwisfloating"> true </item>
<Item name = "Android: javaswistranslucent"> false </item>
<Item name = "Android: windownotitle"> true </item>
<Item name = "Android: background"> @ Android: color/Black </item>
<Item name = "Android: windowbackground"> @ null </item>
<Item name = "Android: backgrounddimenabled"> false </item>
</Style>
</Resources>
When calling, use the alerdialog interface class. The dialog interface writes the following code:
Java code
1. Dialog dialog = new dialog (setactivity. This, R. style. Dialog );
2. Dialog. setcontentview (R. layout. test );
3. Dialog. Show ();