Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Resolve Method getContext() in Android

Tags:

android

I am trying to implement GCM Push notification and try to get Android emulator id, and add the following code, but it shows me the following error. I cam relatively new on this platform.

public class MainActivity extends AppCompatActivity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String android_id= Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);

}

enter image description here

like image 657
casillas Avatar asked Dec 08 '15 04:12

casillas


3 Answers

getContext() method available in View class. Use MainActivity.this to access getContentResolver method:

String android_id= Settings.Secure.getString(MainActivity.this.getContentResolver(),
                                             Settings.Secure.ANDROID_ID)
like image 100
ρяσѕρєя K Avatar answered Nov 06 '22 03:11

ρяσѕρєя K


Hey here is the solution for your question:-

You are using activity so You don't need to use getcontext() or this

Just use below line of code to get you the string:-

String string =Settings.Secure.getString( getContentResolver(),Settings.Secure.ANDROID_ID);
like image 23
Hardy Avatar answered Nov 06 '22 04:11

Hardy


You can use this

 String android_id= Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
like image 1
IntelliJ Amiya Avatar answered Nov 06 '22 05:11

IntelliJ Amiya