我想在我的Android應用程序中有2件事。如何在Android中獲取設備ID?
獲取設備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
你在哪裏調用這個方法(Secure.getString(...))?嘗試在setContentView(R.layout.activity_main)後調用它; – Gorets 2013-04-04 09:22:11
我確實在那裏調用它。 – donparalias 2013-04-04 09:26:03