Java WakefulBroadcastReceiver 无法解析为类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19051088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:42:30  来源:igfitidea点击:

WakefulBroadcastReceiver cannot be resolved to a type

javaandroid

提问by Geek

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-libraryand google play services's path. Even my mapView works (if I comment below code) that means google play serviceis correctly added to project.

我的项目显示错误消息是WakefulBroadcastReceiver cannot be resolved to a type. 我查找了所有我能想到的可能原因。我已经设置了android-support-librarygoogle play services的路径。甚至我的 mapView 工作(如果我在代码下面评论)这意味着google play service已正确添加到项目中。

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?

知道为什么这会出错吗?

采纳答案by CommonsWare

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

确保您已将最新的 Android 支持包添加到您的项目中。例如,这个示例项目有一个合适的android-support-v4.jar并演示了WakefulBroadcastReceiver.

回答by Saqib

It's because of android-support-v4.jarfile, 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..

这是因为android-support-v4.jar文件,确保你有最新的。如果你认为你的项目中有它,那就做个小把戏吧。在另一个项目中找到相同的文件,比如使用最新的 SDK 创建新项目。将它的 jar 文件复制到您的此项目中并替换旧的。现在导入import android.support.v4.content.WakefulBroadcastReceiver;并查看它的工作..

回答by Dimitris

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/.

获取最新版本的支持文件。如果您使用的是 Eclipse,请启动 Android SDK 管理器(Window->Android SDK Manager)。在 Extras->Android Support Library 下,您将看到是否有最新版本(如果没有,它会显示更新可用)。更新后的包应该出现在这里:/extras/android/support/。

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

更多信息:http: //developer.android.com/tools/support-library/setup.html

回答by meda

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:

这是因为 Android 支持 JAR 没有像 SDK 管理器那样更新。您可以从 libs 文件夹中删除它,然后执行以下操作:

  1. right click project
  2. Go to Android Tools
  3. Add Support Library
  1. 右键项目
  2. 转到 Android 工具
  3. 添加支持库

Here 's a screenshot enter image description here

这是截图 在此处输入图片说明

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

然后回到你的类命中Ctrl+ Shift+O

It will import your missing library:

它将导入您丢失的库:

import android.support.v4.content.WakefulBroadcastReceiver;

Project > Cleanand Voilà !!

Project > Clean瞧!

回答by Vince Yuan

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

我也有同样的问题。我已经通过 Android SDK 管理器更新了“Android 支持库”,但仍然找不到 WakefulBroadcastReceiver。

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

解决方法是您必须在Android SDK Manager 中删除“Android 支持库”并重新安装它!

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.

我不得不说为 Android 开发并不令人愉快。Google 弃用了 c2dm 和 com.google.android.gcm.GCMBaseIntentService,因此所有关于 android 推送通知的教程都过时了。

回答by Pravin Bhosale

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

它对我有用 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`

你的 jar 文件位置是 sdk\extras\android\support\v-13`

回答by lucasddaniel

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

旧的 v4 .jar 文件已弃用!请卸载并重新安装!将工作 ;)

回答by Abhijit Chakra

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

如果所有答案都不起作用,请将您的目标设置为 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.

如果您错过了将 Google API 设置为目标的情况,这一点很明显。