18

My project shows error message that WakefulBroadcastReceiver cannot be resolved to a type. I looked up for all possible reasons I can think of. I have set android-support-library and google play services's path. Even my mapView works (if I comment below code) that means google play service is correctly added to project.

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

Any idea why this gives error?

Geek
  • 8,280
  • 17
  • 73
  • 137
  • I have the same issue, for some reason this WakefulBroadcastReceiver is not found in the library even though I am using the latest version of the support library. I got a lot of stuff dependent on the support library that works fine, e.g. support library Fragments so I got a working support library for sure. – Tobias Lindberg Sep 27 '13 at 13:04
  • @user1948603 So how did you solve it? – Geek Sep 27 '13 at 13:08
  • I haven't solved it, I have right now exactly same issue like you but in my case (not sure if yours the same) I know I have my support library properly set up because I am using classes all over the place from that library, it's just this WakefulBroadcastReceiver that doesn't want to work for me. – Tobias Lindberg Sep 27 '13 at 13:23
  • @TobiasLindberg Even I use BroadcastReceiver in all activities and that works. Only this one is not working. – Geek Sep 27 '13 at 13:34
  • Well, in this scenerio it is not a normal BroadcastReceiver right? Do you have other code in your project using dependencies from the support library as well? – Tobias Lindberg Sep 27 '13 at 13:38
  • @TobiasLindberg Sorry I use LocalBroadcastReceiver and that uses support library. Moreover, I have integration facebook which uses it, too. – Geek Sep 27 '13 at 13:42

9 Answers9

43

I had this problem today here's how I fixed it:

It's due to the Android Support JAR not being updated like the SDK manager one. You can delete it from your libs folder then do this:

  1. right click project
  2. Go to Android Tools
  3. Add Support Library

Here 's a screenshot enter image description here

Then go back to your class hit Ctrl + Shift + O

It will import your missing library:

import android.support.v4.content.WakefulBroadcastReceiver;

Project > Clean and Voilà !!

meda
  • 45,103
  • 14
  • 92
  • 122
22

Make sure that you have the latest Android Support package added to your project. For example, this sample project has a suitable android-support-v4.jar and demonstrates the use of WakefulBroadcastReceiver.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I use the support library same as my other team mates. It works on their machines. – Geek Sep 27 '13 at 12:57
  • @Akash: Either the JAR is wrong, where you have the JAR is wrong (should be in `libs/` for a traditional Eclipse or Ant project), or perhaps there is a mis-configuration in Eclipse (see http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22/16596990#16596990). – CommonsWare Sep 27 '13 at 13:08
  • @CommonsWare Not sure about Akash but I have same issue like him and I am sure my support library works fine. The support library is added as you say and I am using a lot of stuff fully working from that support library allready like e.g. Fragments, CursorLoaders etc. but for some reason WakefulBroadcastReceiver does not want to be found. – Tobias Lindberg Sep 27 '13 at 13:32
  • How can I check that my jar is wrong? As Tobias says I use other classes from same library such as `LocalBroadcastReceiver` and that works fine. – Geek Sep 27 '13 at 13:37
  • 3
    @TobiasLindberg: That would suggest that you have an older JAR, from before `WakefulBroadcastReceiver` was introduced. – CommonsWare Sep 27 '13 at 13:48
  • 3
    @Akash: Go into the SDK Manager, make sure that you have the latest JAR downloaded into your SDK installation, then make sure that the JAR in your project matches the JAR in the SDK installation. The R18 support package has a JAR with a file size of 556198 bytes and an `md5sum` of ed257a47cae11af1a55614055b879947. That is the same JAR that is in my GitHub repo for the sample project that I have linked to in my answer. You can also examine a JAR's contents via a ZIP utility and see if it contains `WakefulBroadcastReceiver`. – CommonsWare Sep 27 '13 at 13:50
  • @CommonsWare I have it already installed and using the same. Still issue persists. – Geek Sep 27 '13 at 13:59
  • @Akash: Sorry, I have no further suggestions, other than to make sure that you are importing the right class. – CommonsWare Sep 27 '13 at 14:05
  • @Akash I somehow manage to get mine working. First using latest support library didn't work and then I went to Help -> Check for updates and updated all the android related stuff in eclipse followed by updating everything in the SDK Manager, restarted eclipse, opening it again, clean -> refresh, something like that and it started working. – Tobias Lindberg Sep 27 '13 at 14:16
  • Not the most clear obvious solution but that is more or less how I got it working. – Tobias Lindberg Sep 27 '13 at 14:18
  • @TobiasLindberg I already did that. My problem started in between the updates. – Geek Sep 28 '13 at 14:06
  • @TobiasLindberg Do you think that installing eclipse from scratch will solve it? – Geek Sep 28 '13 at 14:18
  • I used the support library in @CommonsWare 's example code. in the first time you copy it, doesn't work. I cleaned the project after that and it worked. – osayilgan Oct 03 '13 at 15:53
  • 6
    Android Studio : Add in build.gradle this line in `dependencies` : `compile 'com.android.support:support-v4:19.0.0'` (before make sure you installed Android Support Library using Android SDK Manager) – Maxence Nov 05 '13 at 14:52
  • 2
    Just to add to the @Laguiz comment: You can now replace 19.0.0 with 20.0.0 as there is a new revision of support library. – gswierczynski Jul 16 '14 at 09:31
  • @VivekWarde: The link in the answer works just fine: https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/WakeCast – CommonsWare Jan 07 '15 at 16:38
  • sorry, right now github server is down perhaps in india . Also github.com does not open.. I'll see it later – Vivek Warde Jan 07 '15 at 17:51
  • @VivekWarde: GitHub had been banned in India a week or so ago, but I thought that ban had been lifted. – CommonsWare Jan 07 '15 at 18:09
  • @CommonsWare thanks for letting it me know. can you please send me your project on my mail vivekwarde9@gmail.com I will upvote your ans – Vivek Warde Jan 08 '15 at 03:23
  • sample project is not available any more – Bernauer May 26 '21 at 16:33
7

It's because of android-support-v4.jar file, make sure you have the latest. If you think you have it in your project just do a trick. Find same file in another project, say create new project with latest SDK. Copy it's jar file into your this project and replace the older one. Now import import android.support.v4.content.WakefulBroadcastReceiver; and see it working..

Saqib
  • 1,120
  • 5
  • 22
  • 40
  • My application uses external library project that by default depends on an outdated `android-support-v4.jar`. Updating (replacing) the outdated `android-support-v4.jar` on external library project did the trick. – Aryo Feb 06 '14 at 02:35
2

Get the latest version of the support file. If you are using Eclipse, start the Android SDK manager (Window->Android SDK Manager). Under Extras->Android Support Library you will see if you have the latest revision (if not it will say update available). After updating the package should appear here: /extras/android/support/.

More info: http://developer.android.com/tools/support-library/setup.html

Dimitris
  • 682
  • 3
  • 14
  • 19
0

I have the same problem. I have updated 'Android support library' via Android SDK Manager but still cannot find WakefulBroadcastReceiver.

The solution is you have to delete 'Android support library' in Android SDK Manager and re-install it!

I have to say developing for Android is not delighting. Google deprecated c2dm and com.google.android.gcm.GCMBaseIntentService so that all tutorials about android push notification become obsolete.

Vince Yuan
  • 10,533
  • 3
  • 32
  • 27
0

It works for me Add v-13 jar file `Go to\projects\properties\java built path\add external jar

And your jar file location is sdk\extras\android\support\v-13`

Pravin Bhosale
  • 1,920
  • 19
  • 14
0

The old v4 .jar file is deprecated! Please uninstall and install again! Will work ;)

lucasddaniel
  • 1,779
  • 22
  • 22
0

The above answer didn't worked for so the below procedure fixed my issue.

IF none of the answers are working set your target to Properties->Android->Under Project build target Select the goolgle API's ->Apply -> OK

This is obvious in case you missed to set Google API as your target.

Abhijit Chakra
  • 3,201
  • 37
  • 66
0

Add below depedancy in your build.gradle file androidx.legacy:legacy-support-v4:1.0.0

Rohit
  • 2,646
  • 6
  • 27
  • 52