プログラムによるユーザーエージェントの取得
-
26-09-2019 - |
質問
を持たずにブラウザのユーザーエージェントを取得する方法はありますか? WebView
活動中?
経由で入手できることは知っています WebView
:
WebView view = (WebView) findViewById(R.id.someview);
String ua = view.getSettings().getUserAgentString() ;
しかし、私の場合、WebView オブジェクトは必要ありませんし、ユーザーエージェント文字列を取得するためだけに作成したくありません。
解決
そうしないと 持っている このように撮ってみてはいかがでしょうか
String ua=new WebView(this).getSettings().getUserAgentString();
編集-
のドキュメント getUserAgentString()
言う
WebView のユーザーエージェント文字列を返します。.
なので、申告しないともらえないと思います。私が間違っていたら誰かが訂正してくれる
他のヒント
ははるかに簡単な方法があります。確かにこれはWebViewのを返しますが、あなたの目的のために十分にお役に立てかもしれないとまったく同じユーザーエージェント文字列ではありません。
ウェブビューから引っ張っへの追加の利点として、あなたはどのスレッド(だけでなく、UIスレッド)からこれを使用することができます。
User-Agent文字列を取得するために使用することができますhttp.agentというシステムプロパティは、あります。
String userAgent = System.getProperty("http.agent");
を参照してください。プログラムのgetのUser-Agent文字列は、多くのためを詳細ます。
以前使用していました 解決 デラガンによって提案されました。しかし、シングルを作成するのは WebView
インスタンスはスレッド「WebViewCoreThread」を開始します。このスレッドは、アプリケーションがシステムによって終了されるまでバックグラウンドに残ります。リソースをあまり消費しないのかもしれませんが、とにかく好きではありません。そこで、現在は少し異なる方法を使用して、WebViewCoreThread の作成を回避しようとしています。
// You may uncomment next line if using Android Annotations library, otherwise just be sure to run it in on the UI thread
// @UiThread
public static String getDefaultUserAgentString(Context context) {
if (Build.VERSION.SDK_INT >= 17) {
return NewApiWrapper.getDefaultUserAgent(context);
}
try {
Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
constructor.setAccessible(true);
try {
WebSettings settings = constructor.newInstance(context, null);
return settings.getUserAgentString();
} finally {
constructor.setAccessible(false);
}
} catch (Exception e) {
return new WebView(context).getSettings().getUserAgentString();
}
}
@TargetApi(17)
static class NewApiWrapper {
static String getDefaultUserAgent(Context context) {
return WebSettings.getDefaultUserAgent(context);
}
}
それは創造します WebSettings
パッケージに表示されるコンストラクターを直接使用してインスタンスを作成し、それが何らかの理由で利用できない場合 (例:将来の Android バージョンでの API 変更のため) - サイレントに「WebView のような」ソリューションにフォールバックします。
アップデート
が指摘したように @Skywalker5446, 、Android 4.2/API 17 以降、デフォルトのユーザー エージェント値を取得するパブリック静的メソッドがあります。サポートされているプラットフォームでそのメソッドを使用するようにコードを更新しました。
アンドロイド2.1ので、あなたがのは、System.getProperty( "http.agent")を使用する必要があります。の
また、利点のthats、最初のWebViewを作成する必要がいけないと あなたは非uithread内でそれを使用することができます。
挨拶スティーブ
Idolonの答えのおかげで私のアプリがバックグラウンドでこれを処理することができます。
しかし、何とか2.3.3を実行しているAT&TからHTCインスパイア4Gには、catchステートメントに行かないと、それはもはやバックグラウンドスレッドで実行することができます。 このため私のソリューションは以下の通りである:
public static String getUserAgent(Context context) {
try {
Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
constructor.setAccessible(true);
try {
WebSettings settings = constructor.newInstance(context, null);
return settings.getUserAgentString();
} finally {
constructor.setAccessible(false);
}
} catch (Exception e) {
String ua;
if(Thread.currentThread().getName().equalsIgnoreCase("main")){
WebView m_webview = new WebView(context);
ua = m_webview.getSettings().getUserAgentString();
}else{
mContext = context;
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
WebView webview = new WebView(mContext);
mUserAgent = webview.getSettings().getUserAgentString();
}
});
return mUserAgent;
}
return ua;
}
}
(あなたがフィールドにmContextとmUserAgentを持っていると仮定)。
これはキットカットのためにコンパイルするときに動作します以前の回答に基づいて更新されたソリューションです。今WebSettings
クラスは抽象的であるとWebSettingsClassic
クラスが削除されています。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getUserAgent(final Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return WebSettings.getDefaultUserAgent(context);
}
else {
try {
final Class<?> webSettingsClassicClass = Class.forName("android.webkit.WebSettingsClassic");
final Constructor<?> constructor = webSettingsClassicClass.getDeclaredConstructor(Context.class, Class.forName("android.webkit.WebViewClassic"));
constructor.setAccessible(true);
final Method method = webSettingsClassicClass.getMethod("getUserAgentString");
return (String) method.invoke(constructor.newInstance(context, null));
}
catch (final Exception e) {
return new WebView(context).getSettings()
.getUserAgentString();
}
}
}