我是android新手。我正在学习android中的gcm集成部分,我遇到了这个问题,我无法调用未在清单中标记为启动器的活动。我能够收到gcm通知和所有数据,但无法调用该活动。为什么会这样?只有当应用程序从后台删除时,我才会遇到此问题。当应用程序未运行或从后台删除时,有没有办法存储和检索应用程序上下文?
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
答案 0 :(得分:0)
在意图
中添加这两行 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
我正在使用此方法,它工作正常,HomeActivity不是我的启动器活动
private void sendNotification() {
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}