[英]Trying to make a broadcast receiver to detect when i change network or disconnect from one
所以我想做一個Android應用程序來監視傳入的數據。 了解每個網絡(Wifi,4g)消耗多少數據。 我目前正在使用Xamarin.Android,對android來說還很陌生。 我在大學學習過comp sci,並且在使用C#和SQL方面有一年的經驗(盡管我還沒有真正寫過代碼)。
我已經弄清楚了如何獲取正在使用的網絡,但是我無法使Broadcastreceiver()能夠檢測網絡的變化。 我發現的唯一解決方案是使用Java,這使其變得更難一點。 我已經翻譯了大多數Java解決方案,這就是我要講的內容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Net;
namespace ProjectData
{
[BroadcastReceiver()]
public class NetworkReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
ConnectivityManager connectivityManager = (ConnectivityManager)
context.GetSystemService(Context.ConnectivityService);
NetworkInfo activeNetInfo =connectivityManager.ActiveNetworkInfo;
if (activeNetInfo != null)
Toast.MakeText(context, "Active Network Type : " + activeNetInfo.TypeName, ToastLength.Short).Show();
}
}
}
現在我的主要問題是如何使用它。 到目前為止,這是我一直在嘗試的方法:
public void Network()
{
NetworkReceiver networkReceiver = new NetworkReceiver();
Intent intent = new Intent(this, networkReceiver.Class);
RegisterReceiver(networkReceiver, new IntentFilter(ConnectivityManager.ConnectivityAction));
}
我無法測試它,因為出現此錯誤:
標記<接收者>屬性名稱具有無效字符''。
對於第10行:
<intent-filter><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver>
這是整個清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ProjectData.ProjectData" android:versionCode="1" android:versionName="1.0">
<!--suppress UsesMinSdkAttributes-->
<uses-sdk android:minSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application android:allowBackup="true" android:label="@string/app_name" android:name="android.app.Application" android:icon="@drawable/dataicon" android:debuggable="true">
<receiver android:name=".NetworkReceiver"> android:exported=true
<intent-filter><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver>
<receiver android:name="md5f626beda2f87a7aeee80e4f58cdf3b35.NetworkReceiver" />
<activity android:icon="@drawable/dataicon" android:label="ProjectData" android:name="md5f626beda2f87a7aeee80e4f58cdf3b35.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="ProjectData.ProjectData.mono.MonoRuntimeProvider.__mono_init__" />
<!--suppress ExportedReceiver-->
<receiver android:name="mono.android.Seppuku">
<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU" />
<category android:name="mono.android.intent.category.SEPPUKU.ProjectData.ProjectData" />
</intent-filter>
</receiver>
</application>
</manifest>
我正在運行Android 7.0的手機上調試它
謝謝您的幫助!
嘗試改變它
<receiver android:name=".NetworkReceiver"> android:exported=true
至 :
<receiver android:name=".NetworkReceiver"
android:exported=true>
您可以使用來自金塊的Xam.Plugin.Connectivity,效果完美
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.