- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 GCM 在发布图片时收到通知,然后我下载并处理它:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
DataUtils.log("In GcmBroadcastReceiver! threadname is " + Thread.currentThread().getName());
// 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);
}
}
这是我的 GcmIntentService 的开始:
public class GcmIntentService extends IntentService
{
public static final int NOTIFICATION_ID = 1;
public GcmIntentService()
{
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent)
{
DataUtils.log("In GcmIntentService onHandleIntent(), threadname is " + Thread.currentThread().getName());
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) // has effect of unparcelling Bundle
{
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types you're
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
{
DataUtils.log("In GcmIntentService - Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
{
DataUtils.log("In GcmIntentService - Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
{
String notificationType = extras.getString(MyAppApi.GCM_MSG_TYPE_KEY);
if(DataUtils.isEmpty(notificationType)) {
DataUtils.log("In GcmIntentService - notificationType is empty!");
} else if(notificationType.equalsIgnoreCase(MyAppApi.GCM_IS_NEW_WALLPAPER)) {
//We're about to receive a new image!
DataUtils.log("In GcmIntentService - Receiving a new image!");
processNewWallpaper();
} else if(notificationType.equalsIgnoreCase(MyAppApi.GCM_IS_FRIEND_NOTIFICATION)) {
//We're about to receive a friend notification
DataUtils.log("In GcmIntentService - Receiving a friend notification!");
processFriendNotification();
} else {
//Unknown
DataUtils.log("In GcmIntentService - Receiving unknown message type! " + notificationType);
}
} else {
DataUtils.log("In GcmIntentService - Unknown GCM message: " + extras.toString());
}
}
//Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
似乎服务会随机死亡。来自日志:
01-13 20:00:44.436: I/ActivityManager(375): Process com.grakk.android (pid 23227) has died.
01-13 20:00:44.444: W/ActivityManager(375): Scheduling restart of crashed service com.grakk.android/.GcmIntentService in 11426ms
代码在收到 GCM 消息时所做的是下载图像,然后向用户显示通知(这类似于普通的聊天应用)。
一位测试人员告诉我,一旦他收到一张图片但没有收到通知,这意味着服务本身已经启动并完成了部分工作,但没有完成。
通知代码与图像的下载和处理一起在 processNewWallpaper() 中运行。这是代码:
...
if(senderContact == null) {
sendNotification(null, message, true);
} else {
sendNotification(senderContact.getName(), message.trim(), false);
}
...
通知方式:
...
// Put the message into a notification and post it. This is just one simple example
// of what you might choose to do with a GCM message.
@SuppressWarnings("deprecation")
@TargetApi(16)
private void sendNotification(String name, String message, boolean isAnonymous)
{
Context context = GcmIntentService.this;
NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ContactsActivity.class), 0);
Notification.Builder mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name));
String textToShow = null;
if(DataUtils.isEmpty(message))
{
if(isAnonymous) {
textToShow = context.getString(R.string.notification_text_anonymous);
} else {
textToShow = String.format(getResources().getString(R.string.notification_text_friend), name);
}
} else {
textToShow = message;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mBuilder.setStyle(new Notification.BigTextStyle().bigText(textToShow));
}
mBuilder.setContentText(textToShow);
mBuilder.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} else {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.getNotification());
}
}
我可以通过给自己发送一张图片来重现这一点,然后反复按 Android 后退按钮,直到我不再在应用程序中。我可以按照显示图像已下载的日志消息进行操作,但是它在显示通知之前就死了。
这并不总是会发生。有时显示通知,有时不显示。
我不确定可能的原因是什么,也不知道如何调试它。有什么建议吗?
最佳答案
你在GcmIntentService
类中调用了OnCreate()
吗?
下面是一些示例代码:
public class GcmIntentService extends IntentService {
String mes;
private Handler mHandler;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Recevied: (" + messageType + ") " + extras.getString("title"));
GcmReceiver.completeWakefulIntent(intent);
}
public void showToast() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG).show();
}
});
}
}
编辑:为 GCM
添加有用的 youtube 教程 here .
关于android - GcmBroadcastReceiver/GcmIntentService 死亡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934779/
我使用适用于 Windows 的 QuickBooks。并且 Windows 应用程序调用我的后端,在某些库脚本中它调用 exit 或 die ...所以我无法捕获响应,需要检测退出运行的位置或在退出
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我很无聊,只有 30 分钟的空闲时间,所以我决定尝试一下制作生活游戏。我在维基百科上遵循了该规则,但它似乎无法正常工作。有人可以告诉我我会做错什么吗? 规则如下: 任何少于两个活邻居的活细胞都会死亡,
我的社交网络上有一个简单的插入查询,允许用户在文本区域中输入文本并将其输出到他们的个人资料页面上。他们可以选择删除,并按预期将状态标记为“死亡”,并删除更新。问题是,如果他们只是输入一个新的内容,而不
这可能是一个硬件问题,因为我不确定后台 worker 是如何实现的,但问题是:我有 3 个后台 worker 运行不同的线程。我对这些线程进行了编码,以便在它们开始和结束时显示,但我得到了一些非常奇怪
我有一个简单的服务器: from multiprocessing import Pool, TimeoutError import time import os if __name__ == '__m
在这里,我尝试在线程终止后加入该线程,代码工作正常,但我的问题是它不应该抛出一些错误消息或任何信息吗? public class MultiThreadJoinTest implements Runn
我正在开发一个简单的 Android 项目,但遇到了可怕的致命异常,并且确实不知道如何确定我是否在 Android 世界中做一些越界的事情...... 我有这个代码: private OnClickL
我正在尝试编写一个程序,根据用户输入值“n”,我将 fork 那么多进程以同时运行。此外,每个 child 都试图执行我创建的另一个文件,这将生成 x sleep 时间和 x 退出值。最终,当 chi
我们知道,当系统资源耗尽时,后台 Activity 会序列化其状态并被操作系统杀死。当我们恢复它时,操作系统通过传递给 onCreate 方法的 savedInstanceState 恢复 Activ
我想这样做是为了测试我的代码。我想我可以围绕弱引用对象制作另一个包装器,但更愿意直接工作以模拟原始对象被回收。 这是我目前的代码 var myString = "someString"; var
我使用 GCM 在发布图片时收到通知,然后我下载并处理它: public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
我有一个带有中央事件机制的程序,该机制使用 select、epoll 或 kevent(平台依赖、OSX、Linux 和 FreeBSD)。 现在我有一个问题,我需要等待一个进程完成执行。我从这个过程
我有一个非常简单的函数,如下所示: public static boolean isAppInstalled(String packageName) { PackageManager pm =
我有一个接受图像复制/粘贴的 TinyMCE 表单。输入完评论后,按“发布”,它会提交表单,解析内容并显示在屏幕对话中。 我正在尝试使用 preg_match_all 从 POSTed 变量中提取 b
这是我在 vs 代码中的交互式窗口中运行代码时显示的错误消息: C:\Users\rohit\AppData\Roaming\Python\Python38\site-packages\traitle
在 Scala 中,当另一个(远程)actor 终止时,可以通过设置 trapExit 标志并使用第二个 actor 作为参数调用 link() 方法来通知一个 actor。在这种情况下,当远程参与者
我正在tpot上运行dask在gcp的kubernetes集群上运行,该集群是24核120 GB内存,带有4个kubernetes节点,我的kubernetes yaml是 apiVersion: v
几周来我一直在为这个问题苦苦挣扎。我对 Android 还很陌生,希望您能帮助我。 我有这个服务,它运行在与应用程序不同的线程上。本质上,用户指示它启动,并且它应该保持 Activity 状态,直到用
我正在编写一个插件,它从不和谐中获取消息并将其发送到我的世界服务器。Minecraft 客户端很难渲染表情符号。因此我选择使用https://github.com/kcthota/emoji4j将所有
我是一名优秀的程序员,十分优秀!