android - 我如何在 Android 中获取设备 ID?

标签 android webview device

我想在我的 Android 应用程序中包含 2 个东西。

获取设备id,然后用这个id打开一个网页。例如 myserver.com/deviceId

我的主要 java 类如下所示:

package es.unican.CityInfo;

import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.provider.Settings.Secure;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 


        WebView mywebview = (WebView) findViewById(R.id.webview);
        mywebview.loadUrl("http://www.google.com");

        //enabling Javascript
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //opening links in my webview. if you delete this line , any link pressed will cause the browser to start
        mywebview.setWebViewClient(new WebViewClient());

    }


}

为了获得我读到的设备 ID,我应该这样做:

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

但是我不明白我应该把 android_id 代码放在哪里,因为我总是得到一个错误。

错误是:

Multiple markers at this line:

- The method getcontext() is undefined for the type MainActivity
- Illegal modifier for parameter android_id; only final is permitted

最佳答案

用这个替换你的错误行:

String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID); 

关于android - 我如何在 Android 中获取设备 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15807090/

相关文章:

Android 抽屉导航样式

android - 表面 View 相机预览

AndroidStudio 2.1.1 在 xml 中所做的更改不会在运行时更新

macos - OSX 上的 WebView 与 WKWebView

android - 应用程序未在 Android 设备上运行

networking - 在 LAN 上实现设备发现的最佳方法是什么

java - 日期 ArrayList 的排序

java - Android webView shouldInterceptRequest - 停止加载资源

iphone - 链接到 WebView 内的资源 - iPhone

iphone - 如何使用 USB 数据线在设备上测试 iOS 应用程序