从后台激活应用程序后Android意图出现问题
我刚刚开始Android开发。我已经按照以下指南在Eclipse中创建了一个连接应用引擎的Android项目: 应用程序可以工作,但当任务转到后台,然后通过接收GCM消息再次激活时,GCMinentService类调用的意图不会到达相应的活动。有什么问题吗从后台激活应用程序后Android意图出现问题,android,android-intent,android-activity,background,google-cloud-messaging,Android,Android Intent,Android Activity,Background,Google Cloud Messaging,我刚刚开始Android开发。我已经按照以下指南在Eclipse中创建了一个连接应用引擎的Android项目: 应用程序可以工作,但当任务转到后台,然后通过接收GCM消息再次激活时,GCMinentService类调用的意图不会到达相应的活动。有什么问题吗 public class GCMIntentService extends GCMBaseIntentService { [...] @Override public void onMessage(Context
public class GCMIntentService extends GCMBaseIntentService {
[...]
@Override
public void onMessage(Context context, Intent intent) {
sendNotificationIntent(context, "Message received via Google Cloud Messaging:\n\n" + intent.getStringExtra("message"), true, false);
}
[...]
private void sendNotificationIntent(Context context, String message, boolean isError, boolean isRegistrationMessage) {
Intent notificationIntent = new Intent(context, RegisterActivity.class);
notificationIntent.putExtra("gcmIntentServiceMessage", true);
notificationIntent.putExtra("registrationMessage", isRegistrationMessage);
notificationIntent.putExtra("error", isError);
notificationIntent.putExtra("message", message);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(notificationIntent);
}
[...]
}
提前谢谢 //添加权限
// add permission <uses-permission android:name="android.permission.WAKE_LOCK" />
@Override
public void onMessage(Context context, Intent intent) {
sendNotificationIntent(context, "Message received via Google Cloud Messaging:\n\n" + intent.getStringExtra("message"), true, false);
context.sendBroadcast(intent) ;
}
private void sendNotificationIntent(Context context, String message, boolean isError, boolean isRegistrationMessage) {
Intent notificationIntent = new Intent(context, RegisterActivity.class);
notificationIntent.putExtra("gcmIntentServiceMessage", true);
notificationIntent.putExtra("registrationMessage", isRegistrationMessage);
notificationIntent.putExtra("error", isError);
notificationIntent.putExtra("message", message);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = neNotification(R.drawable.ic_launcher,"Title",System.currentTimeMillis());
PendingIntent intents = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
notification.setLatestEventInfo(context , context.getString(R.string.app_name), tickerText , intents );
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(100, notification);
}
public class AppBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction() == "AppReceiver") {
// this intent is gcminit intent and you can get data from this Intent
}
}
@凌驾
公共消息无效(上下文、意图){
sendNotificationIntent(上下文,“通过Google云消息传递接收的消息:\n\n”+intent.getStringExtra(“消息”),true,false);
发送广播(意图);
}
私有void sendNotificationIntent(上下文上下文、字符串消息、布尔isError、布尔isRegistrationMessage){
Intent notificationIntent=新的意图(上下文、RegisterActivity.class);
notificationIntent.putExtra(“GCMinentServiceMessage”,true);
notificationIntent.putExtra(“注册消息”,即注册消息);
notificationIntent.putExtra(“错误”,iError);
notificationIntent.putExtra(“消息”,消息);
notificationIntent.addFlags(Intent.FLAG\u活动\u新任务);
NotificationManager NotificationManager=
(NotificationManager)context.getSystemService(context.NOTIFICATION\u服务);
通知通知=通知(R.drawable.ic_启动器,“Title”,System.currentTimeMillis());
pendingent intents=pendingent.getActivity(上下文,0,notificationIntent,Intent.FLAG_ACTIVITY_带到_前面);
notification.setLateStevenInfo(context,context.getString(R.string.app_name),tickerText,intents);
notification.flags |=notification.FLAG_AUTO_CANCEL;
通知经理通知(100,通知);
}
公共类AppBroadcastReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
if(intent.getAction()=“AppReceiver”){
//此意图是gcminit意图,您可以从该意图中获取数据
}
}
}
//在menifest文件中
如果活动在后台,则目的是在onNewIntent中接收,它将在onResume之前调用,因此您必须覆盖onNewIntent
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
boolean b = intent.getBooleanExtra("gcmIntentServiceMessage", false);
........
}
问题几乎解决了。。。必须补充:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
只剩下一个问题:当GCM消息将活动带到前端时,onNewIntent()仍然没有被激发。我将代码放在onCreate()中。收到消息时,您是否正在获取唤醒锁。如果没有,请这样做。另外,从设计的角度来看,您应该使用挂起的意图。因为根据这个密码。当收到消息时,应用程序将启动。如果用户正在做其他事情,您不能用自己的任务替换他的任务。如何获得唤醒锁?什么是悬而未决的意图?我不熟悉Android开发。获取唤醒锁并不能解决问题修改文本和通知图标,这可能会解决问题。但我认为我必须区分应用程序处于活动状态和处于后台状态。如果我只是将代码添加到我的类中,状态栏中会出现一个符号,但原始消息会在某个地方丢失…在新消息到达时发送广播。我与BrodcastReceiver相处不好。。。我在哪里创建和注册它?IntentFilter应该是什么样子?我刚刚开始Android开发。这就是问题所在。。。当活动转到后台并被GCM消息唤醒时,将不再调用onNewIntent()。在活动进入后台之前,onNewIntent()按预期工作。该活动可能会被系统终止,因此您需要onCreate和onNewIntent中的代码
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);