我是android新手。我正在学习android中的gcm集成部分,并且遇到了这个问题,我无法调用未在清单中标记为启动程序的活动。我能够收到gcm通知和所有数据,但无法呼叫该活动。这是为什么发生?只有当应用程序从后台移除时,我才面临此问题。当应用程序没有运行或从后台移除时,是否有办法存储和检索应用程序上下文?如何从GcmListenerService调用非启动类?
public class NotificationsListenerService extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
//String message = data.getString("message");
String message = data.getBundle("notification").getString("body");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
Log.d(TAG, "GCM DATA: " + data);
try{
String CODE=data.getString("code");
if(CODE!=null){
switch (CODE){
case "666":
String id=data.getString("id");
Code666(id);
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
}
private void Code666(String _id) {
Context context = getBaseContext();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("_id",_id);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.floatinglogo)
.setContentTitle("Horn OK")
.setContentText("HelpNeeded!")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
在此先感谢! Sidharth
尝试用'flag_activity_new task'东西。 –
发布相关代码。 –
请发送您正在发送待定意图并显示通知的代码。 –