egydeveloper
egydeveloper

Reputation: 585

error when used getApplicationContext()

I had this error :

Cannot make a static reference to the non-static method getApplicationContext() from the type ContextWrapper

please find the method that have the error registerInGCMService(Context context)

class:

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings.Secure;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    private static final String GCM_SENDER_ID = "1111111111";

    public GCMIntenetService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            String regId = "";
            Log.i(TAG, "unregistering device (regId = " + regId + ")");
            GCMRegistrar.setRegisteredOnServer(context, false);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.i(TAG, "Received message");
        Log.i(TAG, "EXTRAS" + arg1.getExtras());
        // String message = getString(R.string.gcm_message);
        generateNotification(arg0,
                arg1.getStringExtra("Please download our new updates"));
        // notifies user about message

    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    public static void registerInGCMService(Context context) {

        GCM_SENDER_ID = Secure.getString(context.getApplicationContext().getContentResolver(),
                Secure.ANDROID_ID);


        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }

    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}

Upvotes: 2

Views: 10478

Answers (3)

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

Sometime this error comes, when we are using "getConTentResolver()" in static method like:

 public static void Mthd()
 {
   Cursor cursor =getContentResolver().query(uri, null, null, null, null);
   //ur next code
  }

So, in this case it will give an error, Therefore we have to make the function non-static.

Upvotes: 0

Praveenkumar
Praveenkumar

Reputation: 24476

Or you can try like this -

GCM_SENDER_ID = Secure.getString(context.getContentResolver(),
            Secure.ANDROID_ID);

Upvotes: 1

njzk2
njzk2

Reputation: 39397

What you probably meant is :

context.getApplicationContext()

instead of

getApplicationContext()

Upvotes: 4

Related Questions