如何检查活动中收到的待处理意图?

时间:2016-10-21 09:44:28

标签: android android-intent

检查一下:

private void delayhandler() {
  {
  long delayMillis = 6000;
  new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {
         // TODO Auto-generated method stub
         // user is already login; goto dash board
         Intent intent = getIntent();
         if (intent != null && intent.hasExtra("xxx")) {
            Intent mapActivityIntent = new Intent(getApplicationContext(), 
                                                  LatestOffersActivity.class);
            startActivity(mapActivityIntent);
          } else {
            Intent mapActivityIntent = new Intent(getApplicationContext(),
                                                  HomepageActivity.class);
            startActivity(mapActivityIntent);
          }
         SplashActivity.this.finish();
       }
    }, delayMillis);
  }
}
private void sendNotification(com.fcm.Notification notification) {
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder
            .setSmallIcon(R.mipmap.logo_actionbar)
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getMessage())
            .setSound(defaultSoundUri)
            .setAutoCancel(true);
    int requestID = (int) System.currentTimeMillis();
    notificationBuilder.setContentIntent(getPendingIntent(this, requestID, SplashActivity.class));
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(requestID, notificationBuilder.build());
}
public static PendingIntent getPendingIntent(Context context, int requestID, Class classToLaunch) {
    Intent resultIntent = new Intent(context, classToLaunch);
    resultIntent.putExtra("xxx", "123");

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(classToLaunch);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(requestID, PendingIntent.FLAG_UPDATE_CURRENT);

    return resultPendingIntent;
}

1 个答案:

答案 0 :(得分:-1)

您可以在以下代码中添加收件人活动onCreate()

Bundle extras = getIntent().getExtras();
if (extras == null) {
    Log.d("EXTRA, "extra is not received!");
} else {
    String data = extras.getString("EXTRA_KEY");
}

如果您想将意图发送到前台活动,您可以在onNewIntent()中收到意图值:

protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  setIntent(intent);//must store the new intent unless getIntent() will return the old one

  // recheck the intent
  Bundle extras = getIntent().getExtras();
  String data = extras.getString("EXTRA_KEY");
}