gpt4 book ai didi

android - GcmBroadcastReceiver/GcmIntentService 死亡

转载 作者:太空狗 更新时间:2023-10-29 14:11:20 28 4
gpt4 key购买 nike

我使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com