簡體   English   中英

android中的漫游檢測

[英]roaming detection in android

我試圖檢測漫游激活何時發生。 到目前為止,我已經使用了以下代碼,但是因為我無法對其進行測試,所以我不知道它的正確性

TelephonyManager telephonyManager = TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 

PhoneStateListener cellLocationListener = new PhoneStateListener() {
public void onCellLocationChanged(CellLocation location) {
  if(telephonyManager.isNetworkRoaming()
  {
    Toast.makeText(getApplicationContext(),"in roaming",Toast.LENGTH_LONG).show();
   }
 }
};

telephonyManager.listen(cellLocationListener, PhoneStateListener.LISTEN_CELL_LOCATION);

我寫了這個,認為為了首先激活漫游,信號單元必須改變。 請讓我知道我的推論是否正確,如果不正確,我怎么能做到這一點。

如果您想知道是否有語音漫游,您也可以在 TelephonyManager 中測試isNetworkRoaming() ,即使接收器是由android.net.conn.CONNECTIVITY_CHANGE觸發的。 我認為這也將避免getActiveNetworkInfo()返回 null 時的錯誤。

public class RoamingListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony =
            (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephony.isNetworkRoaming())
            Toast.makeText(context, "Is on TelephonyM Roaming", Toast.LENGTH_LONG).show();
    }
}

我認為您想在 NetworkInfo 類中使用 isRoaming() 。 但首先您要注冊一個更改廣播偵聽器:

<receiver android:name="YourListener">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

這為您提供了一個操作名稱:ConnectivityManager.CONNECTIVITY_ACTION

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
  //...Here check if this is connected and available...
  return ni.isRoaming();
}

[編輯:已知問題] 注意:某些版本似乎存在錯誤,其中 getActiveNetworkInfo() 在漫游時返回 null。 請參閱此處: http : //code.google.com/p/android/issues/detail?id=11866

我希望這有幫助!

伊曼紐爾

漫游狀態更改時獲取事件的可靠方法是

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onServiceStateChanged(ServiceState serviceState) {
                super.onServiceStateChanged(serviceState);
                if (telephonyManager.isNetworkRoaming()) {
                    // In Roaming
                } else {
                    // Not in Roaming
                }
                // You can also check roaming state using this
                if (serviceState.getRoaming()) {
                    // In Roaming
                } else {
                    // Not in Roaming
                }
            }
        };

每當檢測到漫游狀態時,都會使用 serviceStateChanged 方法通知 PhoneListener。

我通過查看 AOSP 源代碼證實了這一點。

CDMA

https://android.googlesource.com/platform/frameworks/opt/telephony/+/master/src/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java

CDMA LTE

https://android.googlesource.com/platform/frameworks/opt/telephony/+/master/src/java/com/android/internal/telephony/cdma/CdmaLteServiceStateTracker.java

全球通

https://android.googlesource.com/platform/frameworks/opt/telephony/+/master/src/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java

檢查方法 pollStateDone() ,這是為 TelephonyManager 設置漫游狀態的地方

就我而言,我已經在使用 TelephonyManager 和 PhoneStateListener 偵聽網絡狀態更改,因此,就像最初的提問者一樣,我想知道是否可以在那里檢測到漫游事件。

(這將是在接受的答案中使用 ConnectivityManager/NetworkInfo 解決方案的替代方法。)

為了檢測變化,最初的問題建議監聽單元格位置的變化,但我想知道這是否不會導致更多必要的事件。

我想漫游狀態的變化只能隨着網絡狀態的變化而發生。

protected PhoneStateListener psl = new PhoneStateListener() {
    public void onDataConnectionStateChanged(int state, int networkType) {
        if (TelephonyManager.DATA_CONNECTED == state)
            isRoaming = teleman.isNetworkRoaming();
                // teleman is an instance of TelephonyManager
    }
}

public static final int pslEvents = PhoneStateListener.LISTEN_DATA_CONNECTION_STATE;

TelephonyManager teleman = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
teleman.listen( psl, pslEvents );

這種方法有兩個警告,但我認為 ConnectivityManager 解決方案(在已接受的答案中)具有相同的問題:

  1. 我正在監聽 DATA 網絡狀態的變化。 如果用戶沒有建立數據連接(例如他們只是語音漫游),那么它將無法工作,但我認為這不是問題,因為我們大多數人都希望檢測數據漫游(例如,我們的應用程序不用戶漫游時不使用數據)。

  2. 我不確定當用戶開始漫游時數據連接狀態一定會發生變化 - 這只是我的理論,如果有人知道得更好,我很想聽聽。

最后,讓我補充一點,聽 PhoneStateListener::onServiceStateChanged() 可能是最好的解決方案:簡單且不必要的事件最少(這應該很少觸發)但我發現文檔含糊不清,不能判斷 PSL 事件是因 ServiceState 對象的任何屬性(例如,包括漫游)的更改而觸發,還是僅因 ServiceState 的服務狀態屬性而觸發。

隨着 Android 5.1 更新,現在有一個非常簡單的漫游檢測:只需使用SubscriptionManager並從SubscriptionInfo getDataRoaming()獲取漫游信息。 如果有多張 SIM 卡,則只返回活躍的 SIM 卡的信息,這很方便。

這可以完美運行,沒有任何偵聽器 1 用於漫游,0 用於非漫游

try {
            int roaming=    Settings.Secure.getInt(getContentResolver(), Settings.Secure.DATA_ROAMING);
            Log.e("roaming","roaming"+roaming);

        }
        catch (Exception e)
        {
            Log.e("Exception","Exception"+e.getLocalizedMessage());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM