私のアプリは、モバイルデータを介して接続する必要がある場合があります。私は特定のアプリがモバイルデータだけを使用できるようにするアプリがあることを知っています。私はアプリでWi-Fiをどのようにオフにできるのか知っています。しかし、私のアプリにモバイル接続を使用するように指示する方法はありますか?AndroidアプリにWi-Fiをオフにせずにモバイルデータを使用させる
0
A
答えて
0
私はルート付き電話機でのみ機能する回避策を使用します。
setMobileDataEnabledメソッドがConnectivityManagerから削除されましたが、この機能のためにgetDataEnabledとsetDataEnabledの2つのメソッドがTelephonyManagerに追加されました。
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
しかし、あなたはそうしないと、SecurityExceptionを取得するマニフェストファイルにこの権限(MODIFY_PHONE_STATE)を追加する必要があります。
0
アプリごとに通信チャネルを明示的に強制することはできません(ConnectivityManager.setNetworkPreference(...)
で優先モードの使用をリクエストできますが、強制的に使用することはできません)。
// and to be sure:
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
関連する問題
- 1. Androidアプリでのみモバイルデータを使用
- 2. AndroidアプリにWifiを使用してAndroid以外のアプリからデータを受信させる
- 3. wifi P2Pをネットワークに接続せずに
- 4. YouTubeアプリをインストールせずにyoutube APIを使用するには?
- 5. AirPrintを使用せずにiPadアプリからプリントするには?
- 6. requireを使用せずにelasticsearch.jsを動作させるには?
- 7. HTMLファイルをアプリにコピーせずにHTMLページをAndroidアプリにリンクする
- 8. rawQuery()を使用せずにAndroidでクエリに関連するテーブル
- 9. プログラムでWiFiホットスポットをオン/オフにする
- 10. Android Studioを使用せずにAndroidアプリを作成するにはどうすればよいですか?
- 11. フレームワークを使用せずにメニューバーを反応させる方法
- 12. AndroidManifest.xmlに属性を使用せずにXamarin Androidを使用する
- 13. Android:.apkを使用せずにWiki tudeフレームワークを使用するには?
- 14. アプリRadbeaconを使用せずにビーコンを検出しますか?
- 15. Rails Webアプリからsmtpを使用せずにメールを送信
- 16. android listviewスクロールを使わずにアイテムを表示させる
- 17. インテントを使用せずにバンドルを移動する - android
- 18. AndroidプログラムはWifi経由でリモートソケットに接続しますが、モバイルデータ経由では接続しません。
- 19. Android - プログラムで使用されるモバイルデータを確認する方法
- 20. Cocoaアプリ:アプリを終了せずにウィンドウを閉じるには?
- 21. アプリデータを消去せずにAndroidでAdobe Airアプリをデバッグ
- 22. モバイルデータとGPSのオン/オフをプログラム的に - 5.0より上のAndroidも
- 23. データベースを使用せずにiphoneアプリに情報を保存する方法
- 24. モバイルデータを使用すると、Phonegapアプリの起動時間が遅くなりますが、WiFiで高速です
- 25. blobstore.create_upload_urlを使用せずにBlobstoreにアップロード
- 26. Eclipseを使用せずにAndroidアプリをデバッグするにはどうすればよいですか?
- 27. pyWin32をインストールせずに使用する
- 28. Android:ネイティブカメラアプリケーションを使用せずにカメラを開く
- 29. AndroidのSmsManagerを使用せずにSMSを送信..ネイティブライブラリ?
- 30. 外部アプリにAPIを強制的に使用させる
何が管理者に必要ですか?あなたの最初の行は何もしませんか? – Ginso
はいコードを編集しましたが、その時点でWi-Fiをオフにする必要があります – Pitty