您好我正在使用我正在使用GCM概念的Android应用程序。一旦应用收到通知,然后点击我即可启动MainActivity。
如果MainActivity已在运行并且应用程序收到通知,那么已经运行的MainActivity应该完成并重新启动,如果它没有运行,那么只需启动它。
应该有旗帜来做同样的事情。任何人都可以帮助我那个旗帜吗?
GcmIntentService.java
。
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GcmIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
String message = extras.getString("message").toString();
String notificationTitle = extras.getString("title");
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(message, notificationTitle);
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// 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.
private void sendNotification(String msg, String notificationTitle) {
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("notification_message", msg);
notificationIntent.putExtra("notification", true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setDefaults(-1);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)System.currentTimeMillis(), mBuilder.build());
}
答案 0 :(得分:1)
如果你的MainActivity
没有运行,它将被打开/启动(这里没问题)。如果它已在运行,则将调用onNewIntent()
方法(在您的MainActivity
中)。所以你可以按如下方式覆盖它:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
init(); //this method will reload the data, or whatever you want to do
}
答案 1 :(得分:0)
试试这个:
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(sender)
.setContentText(message);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(LoginScreen.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
// mId allows you to update the notification later on.
mNotificationManager.notify(msg_ID,notification);
答案 2 :(得分:-1)
在您的主要活动中执行此操作以启动活动:
Intent notificationIntent = new Intent(this, MainActivity.class);
startActivity(notificationIntent);
finish();
在您的通知活动中,重新打开主要活动:
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
finish();
答案 3 :(得分:-1)
首先需要致电finish()
,然后重新启动MainActivity
finish(); //Close current activity
startActivity(getIntent()); //Restart it