- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经阅读了在这里找到的关于 WakeLock 的各种解决方案,例如在 AndroidManifest.xml 中添加 Permission 以及确保将 GCMIntentService 的 Constructor 设置为 public。
但是,我的代码仍然停留在“获取唤醒锁”
完全相同的代码作为单独的 Android 应用程序单独运行。
我只是在将它集成到我的主应用程序后才遇到此错误。
以下是我的 GCM Intent Service 代码:
package com.mp2012.ieatishootipostanalyzer.notifications;
import static com.mp2012.ieatishootipostanalyzer.notifications.CommonUtilities.SENDER_ID;
import static com.mp2012.ieatishootipostanalyzer.notifications.CommonUtilities.displayMessage;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
import com.mp2012.ieatishootipostanalyzer.R;
import com.mp2012.ieatishootipostanalyzer.notifications.MainActivity2;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, "Your device registred with GCM");
Log.d("NAME", MainActivity2.name);
ServerUtilities.register(context, MainActivity2.name, MainActivity2.email, registrationId);
}
/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity2.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
主要 Activity
package com.mp2012.ieatishootipostanalyzer.notifications;
import static com.mp2012.ieatishootipostanalyzer.notifications.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static com.mp2012.ieatishootipostanalyzer.notifications.CommonUtilities.EXTRA_MESSAGE;
import static com.mp2012.ieatishootipostanalyzer.notifications.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gcm.GCMRegistrar;
import com.mp2012.ieatishootipostanalyzer.R;
public class MainActivity2 extends Activity {
// label to display gcm messages
TextView lblMessage;
// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Connection detector
ConnectionDetector cd;
public static String name;
public static String email;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity2.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Getting name, email from intent
Intent i = getIntent();
name = i.getStringExtra("name");
email = i.getStringExtra("email");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, name, email, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
/**
* Receiving push messages
* */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
@Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
} catch (Exception e) {
Log.e("UnRegister Receiver Error", "> " + e.getMessage());
}
super.onDestroy();
}
}
以下是AndroidManifest xml文件的代码
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.mp2012.ieatishootipostanalyzer.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mp2012.ieatishootipostanalyzer.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".login.DashboardActivity"
android:label="IEatIShootIPost" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".login.RegisterActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.LogoutActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.SuccessRegister"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.LoginActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.CalorieProgressActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.CalculateDRCIActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.WIETActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.NewFoodActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.PieChartActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name="org.achartengine.GraphicalActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".facebook.MainActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".notifications.MainActivity2"
android:configChanges="orientation|keyboardHidden"
android:label="IEatIShootIPost"
/>
<activity
android:name=".notifications.RegisterActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.DashboardBurnCalorieActivity"
android:label="IEatIShootIPost"
/>
<activity
android:name=".login.ProgressBurnCalorieActivity"
android:label="IEatIShootIPost"
/>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.mp2012.ieatishootipostanalyzer" />
</intent-filter>
</receiver>
<service android:name=".notifications.GCMIntentService" />
</application>
最后但同样重要的是,来自 logcat 的错误
Registering app com.mp2012.ieatishootipostanalyzer of senders 74463753470
onReceive: com.google.android.com.c2dm.intent.REGISTRATION
GCM IntentService class: com.mp2012.ieatishootipostanalyzer.GCMIntentService
Acquiring wakelock
它挂起....
谢谢!
最佳答案
将所有GCM
相关的类放在主包中,即调用gcm
注册的地方。在你的项目包名 com.mp2012.ieatishootipostanalyzer
中这样。
关于安卓编程 : GCMIntentService Stuck at WakeLock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064337/
Weblogic 10.3.6 管理控制台有两个与卡住线程时间配置相关的参数。 其中一个:Servers -> Some_Server -> Configuration -> Tuning 具有参数:
我正在使用 lift-json 将 JSON 字符串反序列化为 Scala 案例类。我正在解析的 JSON 有一个共同的结构:数据、成功和错误字段,其中数据包含有趣的位。我已经创建了一个 APIRes
我偶尔会做一名 VBA 程序员,只是为了好玩(不是我的工作)。 我在 MS Excel 2010 中有一系列 VBA 模块。无法弄清楚我做错了什么。这个例程奏效了,然后我改变了一些东西,它就不再起作用
给定以下 C 代码: struct list_element { struct list_element * next; }; typedef struct list_element list
我正在尝试创建一个类似乒乓球的游戏,并且我已经开始实现一些有关 Racket 运动的代码。桨本身的运动一切正常。然而,当我突然改变桨的方向时(更具体地说,当我立即从向上移动桨切换到向下移动桨时,反之亦
我是第一次在网站上手工编码(主要是打印设计师)。我试图只使用 HTML 和 CSS,因为我没有时间深入研究任何 Javascript,而且我真的很想了解我在使用什么。因此,我的主要内容设置在图像网格中
我正在制作一款类似 rogue 的游戏,我正在使用网格内的随机游走来形成“洞穴”系统。然而,我提出的随机游走会卡住——尤其是当游走者靠近网格边缘并且被“洞”包围时。 这不是我在我的项目中使用的确切代码
我在将我的分支推送到远程存储库时遇到问题。 Git 返回一个错误:错误:dst refspec refs/heads/XXX 匹配多个。 当我运行 git ls-remote 时,它只显示一个 XXX
我在终止集群中的命名空间时遇到问题,它在命名空间 JSON 中显示了许多参数。我点击了这个链接 https://medium.com/@craignewtondev/how-to-fix-kubern
首先,我为非描述性标题道歉。因为我不知道实际发生了什么,所以我不能让它更具体。 现在我的问题。我已经为 99 Haskell problems 的问题 23 实现了以下片段,应该随机选择n列表中的项目
尝试解决 eqb_trans 我陷入困境: Theorem eqb_trans : forall n m p, n =? m = true -> m =? p = true -> n =?
我曾经想在Grails中记录sql查询,所以我在数据源中添加了 logSql = true 并在log4j中 trace 'org.hibernate.type' debug 'org.hiberna
我在一个相互碰撞的世界中有几个球。世界各地都有静墙。 有时,它们最终会处于沿同一路径向上/向下或向左/向右移动的位置(垂直于墙壁的运动?) 有什么简单的方法可以检测到这一点,然后在任一方向上对其进行一
我有两个 MySQL 服务器,它们在同一组复制上运行。设置已通过以下步骤完成: 第一台服务器是有大量数据的生产服务器。 我将它设置为只读并转储数据,然后在后备 MySQL 服务器上恢复它 恢复后,我执
编辑:我意识到我的OP中的代码又长又难读。我用 4 行代码突出显示了这个问题。 char **t = {"Hello", "World"}; char **a = t; ++(a[0]); print
我试图将图像上传到服务器,但不知何故代码停在了 FileInputStream 行。不知道为什么,我不知道如何调试或检查它。这是我的源代码: public class CreateSetcardSt
规范:Ubuntu 13.04、Python 3.3.1 一般背景:Python 初学者; 特定问题的背景:我已经精疲力竭地试图解决这个问题,而且我知道,除了它对学习 Python 的指导值(valu
我创建了这个递归脚本,用于检查您所在的地址,然后检查另一个文件层次结构(如果您所在的文件夹也存在于该位置)。例如,假设您在 somerandomsite.com/example/folder/fold
我制作了视频来向您展示确切的问题(向上)。我有一个卡片动画。第一个动画是当您加载页面时,这些卡片会自动飞入。 .card{ animation: startup .5s ease-in-out .2s
简介: 我研究了 blocking TCP server 的 MSDN 示例和 blocking TCP client . 鉴于修改这些示例以创建简单的聊天应用程序,我想尝试一些简单的事情。 首先,我
我是一名优秀的程序员,十分优秀!