我正在尝试在我的Android应用程序中包含两件事。

获取设备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);

09-27 15:27