0

Everyone is talking about this code:

import android.provider.Settings.Secure;

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

When I write this, there will be error like:

"The method getContext() is undefined for the type Bridge"

How can I fix that?

4 Answers4

0

This should solve the issue too,

String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
0

Have you tried using getApplicationContext() instead of getContext()?

package de.vogella.android.deviceinfo;

    import android.app.Activity;
    import android.os.Bundle;
    import android.provider.Settings.Secure;
    import android.widget.Toast;

    public class ShowDeviceInfo extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            String deviceId = Secure.getString(this.getContentResolver(),
                    Secure.ANDROID_ID);
            Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
        }
    } 

http://blog.vogella.com/2011/04/11/android-unique-identifier/

These links might help:

http://developer.android.com/reference/android/app/Activity.html

getApplication() vs. getApplicationContext()

Difference between getContext() , getApplicationContext() , getBaseContext() , getContext(), and "this"

Community
  • 1
  • 1
Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
0

Firstly you should pass an instance of Context into your Bridge class.
To achieve this, you can pass a Context to your constructor, or wherever you want.

As you know, every method belongs to some class in Java. So when you call the method not from some object, like you do with getContext(), this is equal to calling this.getContext(). So you try to call the method from the instance of a class you are operation in now (Bridge), which does not have such a method defined. That's why you get the error

The method getContext() is undefined for the type Bridge

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
0

I had the same issue.

It was that my class was not extending AppCompatActivity as getContentResolver() is a method which resides in that class.

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
Karel Slow
  • 51
  • 2