簡體   English   中英

從廣播接收者到活動的待定意圖 - 收到的舊數據

[英]Pending Intent from Broadcast Receiver to Activity - old data received

我發現了類似的問題,但沒有一個解決方案對我有幫助。 我有一個廣播接收器攔截短信並將短信文本轉發給另一個活動。 其他活動應顯示最新收到的文本。 但另一項活動是始終顯示應用程序首次啟動后收到的第一個文本。 不顯示新的短信文本。

我嘗試過:使用Pending Intent參數(獨特的隨機int和所有4個更新選項) - 到目前為止沒有幫助。

PendingIntent contentIntent = PendingIntent.getActivity(context, **rand.nextInt(50) + 1**, intent, **FLAG_UPDATE_CURRENT**);

這是來自廣播接收器的相關代碼,同時觸發的通知每次都顯示新的正確文本。

    private void showNotification(Context context) {
    Intent intent = new Intent(context, DisplayMSG.class);
    intent.putExtra(EXTRA_MESSAGE, str);
    Random rand = new Random();
    PendingIntent contentIntent = PendingIntent.getActivity(context, rand.nextInt(50) + 1, intent, FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("RPT Notification")
            .setContentText(str + Integer.toString(n));
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}  
}

這是接收活動的一部分:

Intent intent = getIntent();
String message = intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.tView);
textView.setText(message);

如果您需要更多代碼,請與我們聯系。 但我希望這應該足夠了,因為它基本上是有效的,它只是沒有轉移新的文本。

編輯:感謝CommonsWare這里是解決方案,我剛剛將這個方法添加到接收活動中:

@Override
protected void onNewIntent(Intent intent) 
{
    super.onNewIntent(intent);
    TextView textView = (TextView) findViewById(R.id.tView);
    textView.setText(intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE));
}

這是接收活動的一部分

如果活動已經在運行,那就是錯誤的Intent 覆蓋onNewIntent()並使用傳遞給它的Intent getIntent()始終返回用於創建活動的Intent ,而不是任何將現有實例帶回前台的后續內容。

暫無
暫無

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

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