1

I am trying to have 2 things in my Android application.

Get the device id and then open a webpage with this id . eg myserver.com/deviceId

My main java class looks like this :

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 :

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.

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
donparalias
  • 1,834
  • 16
  • 37
  • 60

6 Answers6

2
private String android_id; // declared on top of class

android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID); // called inside onCreate
Carnal
  • 21,744
  • 6
  • 60
  • 75
2

Replace your error line with this:

String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID); 
Utisevalec
  • 156
  • 3
2

Remove private modifier and also don't use getContext() instead use getApplicationContext() or simply getContentResolver()

inside your onCreate() method:

String android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID); 
techroid
  • 477
  • 4
  • 11
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.

Stephan Celis
  • 2,492
  • 5
  • 23
  • 38
Arun C
  • 9,035
  • 2
  • 28
  • 42
1

Change your line in code:

    getContext to getBaseContext
MR. Kumar
  • 661
  • 8
  • 25
0
import android.provider.Settings.Secure;

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