Nine, Android Learning day eighth-broadcasting mechanism and WiFi network operation

Source: Internet
Author: User

(Transferred from: http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html)

Nine, Android Learning day eighth-broadcasting mechanism and WiFi network operation

Today you are familiar with some basic operations of the broadcast mechanism and WiFi network in Android, summarized as follows:

    • Android's broadcast mechanism

We know that in the broadcast mechanism, the sender does not care about the recipient receiving the data or how to process the data.

Here's a summary of the registration methods for Broadcastreceiver in Android:

(i) registering in the application

(ii) Registration in Manifest.xml

If you register in Manifest.xml, the app will receive a broadcast event whether it is on or off. For example: monitor battery power consumption and so on. Obviously, this method of registration is not what we want.

If you register in an application, it is registered when the activity is visible and is unregistered when it is not visible.

The code for registering in the activity is as follows:

Registration: Registerreceiver (broadcastreceiver receiver,intentfilter filter);

Unregistered: Unregisterreceiver (broadcastreceiver receiver);

If a broadcastreceiver is used to update the UI, the method that is registered in the code is typically used.

    • WiFi Basic operation

WiFi should be said to be a more important piece of Android, with Android phone children's shoes, should be in touch with WiFi every day.

Today, I am familiar with how to perform basic operation on WiFi network card, including opening, shutting down, detecting NIC status and so on.

The status of the WiFi card is represented by a series of integer constants, as follows:

A Wifi_state_disabled WiFi card is not available--1

Two Wifi_state_disabling WiFi nic is shutting down--0

Three Wifi_state_enabled WiFi card Available status--3

Four Wifi_state_enabling WiFi nic is opening--2

Five Wifi_state_unknown Unknown NIC status--4

Everyone noticed that after each state I wrote a number, we know that these are integer constants, so the following numbers are the values corresponding to these constants, if you are in the program, output the current status of the card, you will see these values, corresponding to the status of a certain network card.

Operation of the Android WiFi card status, it is more important to operate the WiFi network card required permissions, this has a lot.

We can find the Manifest.permission class in the Android API and we can see the various permissions.

Here is an example of WiFi, this example of WiFi can only be tested on the real machine, because the simulator does not have wifi such functions.

Here are three more important files:

Wifitestactivity.java

Main.xml

Manifest.xml

Here's a look at the specific code in these three files:

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "WIFI"/>
<button
Android:id= "@+id/open"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Turn on WiFi"
/>
<button
Android:id= "@+id/close"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Turn off WiFi"
/>
<button
Android:id= "@+id/check"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Check wifi status"
/>
</LinearLayout>

Wifitestactivity.java

Package Mickey.wifi;

Import android.app.Activity;
Import Android.content.Context;
Import Android.net.wifi.WifiManager;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;


public class Wifitestactivity extends Activity {

Three control objects
Private Button open = null;
Private Button close = null;
Private Button check = null;

We know that in order to operate the NIC, we need to wifimanager this class
Private Wifimanager Wifimanager = null;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Get an instance of the control object and set the appropriate listener for them
Open = (Button) Findviewbyid (R.id.open);
Open.setonclicklistener (New Openwifilistener ());
Close = (Button) Findviewbyid (r.id.close);
Close.setonclicklistener (New Closewifilistener ());
Check = (Button) Findviewbyid (R.id.check);
Check.setonclicklistener (New Checkwifilistener ());
}

To set the listener to open the network card button
Class Openwifilistener implements Onclicklistener {
@Override
public void OnClick (View arg0) {
The inner class, if used directly, represents itself, so use wifitestactivity.this
Get Wifimanager Object
Wifimanager = (Wifimanager) wifitestactivity.this
. Getsystemservice (Context.wifi_service);
Setwifienabled (True) to turn on the WiFi card
Wifimanager.setwifienabled (TRUE);

Get the status of the current network card, here is an integer constant output
System.out.println ("The status of the current network card is:" + wifimanager.getwifistate ());

Pop up a toast prompt user what is the status of the current WiFi card, remember to call the Show method
Toast.maketext (Wifitestactivity.this,
"The current WiFi card has a status of" + Wifimanager.getwifistate (),
Toast.length_short). Show ();
}
}

//  set listener
class closewifilistener implements onclicklistener {to turn off the network adapter button
@Override
Public void onclick (view arg0)  {
//  get Wifimanager object
wifimanager =  (Wifimanager)  wifitestactivity.this
. Getsystemservice (Context.WIFI_SERVICE);
  Set the network card to be unavailable
Wifimanager.setwifienabled (false);
System.out.println ("The status of the current network card is:"  + wifimanager.getwifistate ());
Toast.maketext (wifitestactivity.this,
"Current WiFi card has a status of"  + wifimanager.getwifistate (),
Toast.length_short). Show ();
}
}

To set the listener for the check NIC status button
Class Checkwifilistener implements Onclicklistener {
@Override
public void OnClick (View arg0) {
Get Wifimanager Object
Wifimanager = (Wifimanager) wifitestactivity.this
. Getsystemservice (Context.wifi_service);
System.out.println ("The status of the current network card is:" + wifimanager.getwifistate ());
Toast.maketext (Wifitestactivity.this,
"The current WiFi card has a status of" + Wifimanager.getwifistate (),
Toast.length_short). Show ();

}
}
}

Manifest.xml

<?xml version= "1.0"  encoding= "Utf-8"?
<manifest xmlns:android= "http ://schemas.android.com/apk/res/android "
package=" Mickey.wifi "
android:versioncode=" 1 "
Android: Versionname= "1.0" &NBSP;>
<uses-sdk android:minsdkversion= " />";
<application
android:icon= "@drawable/ic_launcher"
android:label= "@string/app_name" &NBSP;>
<activity
Android: Label= "@string/app_name"
Android:name= ". Wifitestactivity "&NBSP;>
<intent-filter >
<action android:name=" Android.intent.action.MAIN "&NBSP;/>
<category android:name=" Android.intent.category.LAUNCHER "  />
</intent-filter>
</activity>
</application>

<!--These four permissions must have, otherwise the system will not give users the ability to operate network and NIC permissions--
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
</manifest>

Then children's shoes, the Android-based mobile phone with a USB cable to the computer, remember to open the USB debugging, and then run your program, will automatically run the program on the phone ~

Nine, Android Learning day eighth-broadcasting mechanism and WiFi network operation (GO)

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.