I am trying to have 2 things in my Android application.
我想在我的Android应用程序中有两件事。
Get the device id and then open a webpage with this id . eg myserver.com/deviceId
获取设备ID,然后打开具有此ID的网页。例如myserver.com/deviceId
My main java class looks like this :
我的主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());
}
}
In order to get the device id i read that i should do something like this :
为了得到设备ID我读到我应该做这样的事情:
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
However i dont understand where i should place the android_id code as i always get an error.
但是,我不明白我应该放置android_id代码,因为我总是得到一个错误。
The error is:
错误是:
Multiple markers at this line:
- The method getcontext() is undefined for the type MainActivity
- Illegal modifier for parameter android_id; only final is permitted
6 个解决方案
#1
2
Replace your error line with this:
用这个替换你的错误行:
String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
#2
2
private String android_id; // declared on top of class
android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID); // called inside onCreate
#3
2
Remove private modifier and also don't use getContext() instead use getApplicationContext() or simply getContentResolver()
删除私有修饰符也不要使用getContext()而是使用getApplicationContext()或只是getContentResolver()
inside your onCreate() method:
在你的onCreate()方法中:
String android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
#4
1
Change your line in code:
在代码中更改您的行:
getContext to getBaseContext
#5
1
Use this line
使用此行
String android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
Since you are declaring string inside a method it will be scoped inside it. You can get Activity context by using this.
由于您在方法中声明字符串,因此它将在其中作用域。您可以使用此方法获取Activity上下文。
#6
0
import android.provider.Settings.Secure;
String android_id = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);
#1
2
Replace your error line with this:
用这个替换你的错误行:
String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
#2
2
private String android_id; // declared on top of class
android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID); // called inside onCreate
#3
2
Remove private modifier and also don't use getContext() instead use getApplicationContext() or simply getContentResolver()
删除私有修饰符也不要使用getContext()而是使用getApplicationContext()或只是getContentResolver()
inside your onCreate() method:
在你的onCreate()方法中:
String android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
#4
1
Change your line in code:
在代码中更改您的行:
getContext to getBaseContext
#5
1
Use this line
使用此行
String android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
Since you are declaring string inside a method it will be scoped inside it. You can get Activity context by using this.
由于您在方法中声明字符串,因此它将在其中作用域。您可以使用此方法获取Activity上下文。
#6
0
import android.provider.Settings.Secure;
String android_id = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);