Android Development Step by step 20: Network Settings

Source: Internet
Author: User

Network Settings This is a very important piece in the mobile phone application, because the general application needs to interact with the external network. This article shows a more classic application scenario. For example, I have recently developed a transfer application. This is the need to interact with the network. When the user opens the app, the app first determines whether the user has turned on the WiFi or GPRS network. No then jump to the system's Wireless and network settings interface, when the user set up, I have done a more humane processing here, created a broadcast receiver, because we know that the mobile phone in the open network or receive text messages, will release a broadcast. Once the network is connected, my broadcast receiver will receive the message and then determine if the current transfer application is open, and then automatically jump to the transfer application. is to help the user reopen the app, eliminating the need for the user to turn off the app before reopening the operation. OK, let's start with our experiment.
New network Operation class Networkutil.java

Package com.snda.wallet.utils;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.os.Vibrator;

Import COM.SNDA.WALLET.R;

/**
* <p> Network Settings class </p>
* @author Zhuzhifei
*/
public class Networkutil {
Public Networkutil () {

}

/**
* Determine if the network is connected
*
* @param context
* @return
*/
public static Boolean Checknetworkstatus (context context) {
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo activenetinfo = Connectivitymanager.getactivenetworkinfo ();
return (Activenetinfo = = null)? False:true;
}
/**
* Pop-up box to tell the user is not currently connected to the network, point settings to jump to the Network Settings page, click Cancel Close the dialog box
* @param context
*/
public static void Shownetworkstatus (final context context) {
Alertdialog.builder Builder = new Alertdialog.builder (context);
Builder.settitle ("hint message"). Setmessage (r.string.network_confirm). Setcancelable (False)
. Setpositivebutton (R.string.yes, New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int id) {
Intent Intent = new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS);
Context.startactivity (Intent);
}
}). Setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int id) {
Dialog.cancel ();
}
}). Show ();
Vibrator Vibrator = (Vibrator) context.getsystemservice (Context.vibrator_service);
Vibrator.vibrate (New long[] {5, 12, 9, 19},-1);
}

}
Pay attention to remember to add permission
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.VIBRATE"/>
New network status broadcast receiver Networkstatereceiver.java

/**
*
*/
Package com.snda.wallet.network;

Import java.util.List;

Import Android.app.ActivityManager;
Import Android.app.ActivityManager.RunningTaskInfo;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.net.NetworkInfo.State;
Import Android.widget.Toast;

Import com.snda.wallet.tabs.TransferMainActivity;
Import Com.snda.wallet.utils.LogUtils;

/**
* Network Status Broadcast receiver
* @author Zhuzhifei
*
*/
public class Networkstatereceiver extends Broadcastreceiver {

Private String tag = "Networkstatereceiver";

/*
* Broadcast receivers, receiving mobile phones already connected to the network broadcast
*/
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
Determine network status
Networkinfo info = Intent
. Getparcelableextra (Connectivitymanager.extra_network_info);
state state = Info.getstate ();
LOGUTILS.I (Tag, "type:" + info.gettype ());
LOGUTILS.I (Tag, "state:" + state);
Add one more to determine if the current application is open
String my_pkg_name = "Com.snda.wallet";
Boolean isapprunning = false;
Activitymanager am = (activitymanager) context
. Getsystemservice (Context.activity_service);
list<runningtaskinfo> list = am.getrunningtasks (100);
for (Runningtaskinfo task:list) {
if (Task.topActivity.getPackageName (). Equals (My_pkg_name)
&& task.baseActivity.getPackageName (). Equals (My_pkg_name))
if (Task.baseActivity.getPackageName (). Equals (My_pkg_name)) {
Isapprunning = true;
Break
}
}
If the current application is open, connect to the WiFi or 3g network, jump to the application home page
if (isapprunning) {
Switch (state) {
Case CONNECTED:
Intent intentnew = new Intent (context,
Transfermainactivity.class);
Intentnew.setflags (Intent.flag_activity_new_task);
Context.startactivity (intentnew);
Break
Case connecting:
Toast.maketext (Context, "Network is connecting, wait for you to log in automatically", Toast.length_short)
. Show ();
Break
Case disconnected:
Toast.maketext (Context, "Network is not connected, please first networking", Toast.length_short)
. Show ();

Break
Case disconnecting:
Toast.maketext (Context, "Network is losing connection, please note", Toast.length_short)
. Show ();
Break
Default
Break
}
}
Determine the network type
Switch (Info.gettype ()) {
Case Connectivitymanager.type_mobile:
Break
Case Connectivitymanager.type_wifi:
Break
// }

}

}
Please remember to add permission and register the broadcast receiver
<uses-permission android:name= "Android.permission.GET_TASKS"/>
<application android:icon= "@drawable/icon" android:label= "@string/app_name"
Android:name= ". MyApplication "Android:theme=" @style/theme.spm ">
<receiver android:name= ". Network.networkstatereceiver" >
<intent-filter>
<action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
Application Home Call network operation class to determine network status

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.transfer_main);
if (Networkutil.checknetworkstatus (transfermainactivity.this) ==false)//No networking, first prompts the user networking
{
Networkutil.shownetworkstatus (Transfermainactivity.this);
Return
}
......
}

Android Development Step by step 20: Network Settings

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.