Android (determine whether WiFi is enabled, mobile phone screen status, sdcard is pulled out, set full screen)

Source: Internet
Author: User

First question:Determine whether the mobile phone is currently using a SIM card or WiFiI wrote an encapsulation method, which can be used later:

[Java]
View plaincopyprint?
  1. /**
  2. * Check the Internet is
  3. * Mobile or WiFi
  4. * Add By wangxianming
  5. * In 2012-03-22
  6. */
  7. Private Boolean checkwifi (){
  8. Boolean iswificonnect = true;
  9. Connectivitymanager CM = (connectivitymanager) mcontext. getsystemservice (context. connectivity_service );
[Java]
View plaincopyprint?
  1. // Check the networkinfos numbers
  2. Networkinfo [] networkinfos = cm. getallnetworkinfo ();
  3. For (INT I = 0; I <networkinfos. length; I ++ ){
  4. If (networkinfos [I]. getstate () = networkinfo. state. Connected ){
  5. If (networkinfos [I]. GetType () = cm. type_mobile ){
  6. Iswificonnect = false;
  7. }
  8. If (networkinfos [I]. GetType () = cm. type_wifi ){
  9. Iswificonnect = true;
  10. }
  11. }
  12. }
  13. Return iswificonnect;

 

 

Example 2: determine whether the current mobile phone screen has the option of rotating the screen:

[Java]
View plaincopyprint?
  1. /**
  2. * Accelerometer_rotation ----> explain:
  3. *
  4. * Control whether the accelerometer will be
  5. * Used to change screen orientation.
  6. * If 0, it will not be used unless explicitly
  7. * Requested by the application;
  8. * If 1, it will be used by default
  9. * Unless explicitly disabled by the application.
  10. * Constant value: "accelerometer_rotation"
  11. */
  12. Systemgravity = settings. system. getint (this
  13. . Getcontentresolver (),
  14. Settings. system. accelerometer_rotation); // 1 is open; 0 is close;

 

 

The third is to register a broadcast that listens to the memory card status in the Code:

[Java]
View plaincopyprint?
  1. Intentfilter = new intentfilter );
  2. Intentfilter. addaction (intent. action_media_unmounted );
  3. Intentfilter. addaction (intent. action_media_eject );
  4. Intentfilter. addaction (intent. action_media_removed );
  5. Intentfilter. addaction (intent. action_media_bad_removal );
  6. Registerreceiver (sdcardlistener, intentfilter );

When registerreceiver () is used to register a broadcast, the unregisterreceiver () method appears in pairs.

If the broadcast is registered in the oncreate () method, it is released in the ondestroy () method.

If the broadcast is registered in the onresume () method, it is released in the onpause () method.

 

Write an internal broadcast class in the Code:

[Java]
View plaincopyprint?
  1. <Span style = "font-size: 16px; color: #000000;"> private final broadcastreceiver sdcardlistener = new broadcastreceiver (){
  2. Public void onreceive (context, intent ){
  3. Toast. maketext (summaryappmainactivityactivity. This, R. String. sd_removed, 2000). Show ();
  4. }
  5. }; </Span>

 

Fourth, Full Screen Settings: write a simple method;

[Java]
View plaincopyprint?
  1. <Span style = "font-size: 16px; color: #000000;"> // set the activity is fullscreen
  2. Private void setfullscreen (){
  3. Misfullscreen =! Misfullscreen;
  4. If (misfullscreen ){
  5. Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
  6. Windowmanager. layoutparams. flag_fullscreen );
  7. } Else {
  8. Getwindow (). clearflags (windowmanager. layoutparams. flag_fullscreen );
  9. }
  10. } </Span>
  11. <Span style = "color: # ff6600;"> <strong> <span style = "font-size: 18px;"> so few items are sorted out today, take the time to connect the knowledge! Haha, go to bed. See you next time!
  12. I attended the mobile voice Developer Conference today. I am sorry that Liu Chuanzhi and Li Kaifu leijun were not present. Well, I got a little bit of it. I listened to their on-site interviews!

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.