簡體   English   中英

如何檢查活動中收到的未決意圖?

[英]how can I check the pending intent is received in an activity?

檢查一下:

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;
}

您可以將以下代碼添加到收件人活動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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM