Update recipe using newer APIs.

Docs issue - https://github.com/xamarin/documentation/issues/1883
This commit is contained in:
Tom Opgenorth 2017-01-23 09:27:44 -07:00
Родитель d38fc8894f
Коммит e2277351f7
6 изменённых файлов: 35 добавлений и 42 удалений

Просмотреть файл

@ -1,16 +1,13 @@
using Android.Telephony;
using Android.Renderscripts;
using Android.App;
using Android.Net;
using Android.OS;
using Android.Widget;
using Android.Util;
using Android.Nfc;
namespace NetworkDetection
{
using Android.App;
using Android.Net;
using Android.OS;
using Android.Widget;
[Activity(Label = "Network Detection", MainLauncher = true, Icon = "@drawable/icon")]
{
[Activity(Label = "Network Detection", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
static readonly string TAG = typeof(Activity1).FullName;
@ -20,9 +17,9 @@ namespace NetworkDetection
private ImageView _wifiImage;
TextView _connectionType;
protected override void OnCreate(Bundle bundle)
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(bundle);
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
_wifiImage = FindViewById<ImageView>(Resource.Id.wifi_image);
@ -40,22 +37,22 @@ namespace NetworkDetection
private void DetectNetwork()
{
ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;
bool isOnline = (activeConnection != null) && activeConnection.IsConnected;
NetworkInfo info = connectivityManager.ActiveNetworkInfo;
bool isOnline = info.IsConnected;
Log.Debug(TAG, "IsOnline = {0}", isOnline);
if (isOnline)
{
_isConnectedImage.SetImageResource(Resource.Drawable.green_square);
// Display the type of connection
NetworkInfo.State activeState = activeConnection.GetState();
_connectionType.Text = activeConnection.TypeName;
// Check for a WiFi connection
NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
if(wifiInfo.IsConnected)
_isConnectedImage.SetImageResource(Resource.Drawable.green_square);
// Display the type of connectionn
NetworkInfo.State activeState = info.GetState();
_connectionType.Text = info.TypeName;
// Check for a WiFi connection
bool isWifi = info.Type == ConnectivityType.Wifi;
if(isWifi)
{
Log.Debug(TAG, "Wifi connected.");
_wifiImage.SetImageResource(Resource.Drawable.green_square);
@ -66,8 +63,7 @@ namespace NetworkDetection
}
// Check if roaming
NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
if(mobileInfo.IsRoaming && mobileInfo.IsConnected)
if (info.IsRoaming)
{
Log.Debug(TAG, "Roaming.");
_roamingImage.SetImageResource(Resource.Drawable.green_square);

Просмотреть файл

@ -14,7 +14,7 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v4.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@ -93,12 +93,5 @@
<ItemGroup>
<AndroidResource Include="Resources\Drawable\red_square.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>

Просмотреть файл

@ -3,6 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkDetection", "NetworkDetection.csproj", "{88D4BEB0-FABE-43C8-B8E1-710D4E50D9C2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BF2520DD-B1D4-44F0-A899-244E69999945}"
ProjectSection(SolutionItems) = preProject
readme.md = readme.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU

Просмотреть файл

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="NetworkDetection.NetworkDetection">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22" />
<application android:label="NetworkDetection" android:icon="@drawable/icon">
</application>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="1.0" package="NetworkDetection.NetworkDetection">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="22" />
<application android:label="NetworkDetection" android:icon="@drawable/icon"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

Просмотреть файл

@ -2,7 +2,7 @@
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Mono Runtime Version: 4.0.30319.17020
// Mono Runtime Version: 4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.

Просмотреть файл

@ -1,3 +1,3 @@
This is the sample code for the Android recipe for detecting the current network state.
This is the sample code for the Android recipe for detecting the current network state. This sample is for devices that are running Android 5.0 or higher (API level 21).
[See the recipe at developer.xamarin.com](http://developer.xamarin.com/recipes/android/networking/networkinfo/detect_network_connection)