【问题标题】:How to pass text from notification to another activity?如何将文本从通知传递到另一个活动?
【发布时间】:2014-12-26 12:49:48
【问题描述】:

在我的应用程序中,当我单击列表视图项时将显示一个通知,现在当我单击该通知时打开另一个活动并在另一个活动中显示通知内容。在这里,当我选择列表视图项目通知将附带选定的项目文本。在我选择另一个列表视图项后,通知也会附带相关文本,但确切的问题是当我打开该通知时,另一个活动中只会显示一个文本。请修复它。

rmdListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                    showNotification();

                           }

现在通知方式是

public void showNotification() {
    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // intent triggered, you can add other intent for other actions
    Intent intent = new Intent(updateActivity.this,
            NotificationReceiver.class);        
    intent.putExtra("Name", noti);
    PendingIntent pIntent = PendingIntent.getActivity(updateActivity.this,
            0, intent, 0);  
    Notification mNotification = new Notification.Builder(this)

    .setContentTitle("Reminder!").setContentText(timeList + ":" + noti)
            .setSmallIcon(R.drawable.logosmall).setContentIntent(pIntent)
            .setSound(soundUri)             
            .build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);     
    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}

【问题讨论】:

  • 使用 SharedPref 获得更好的方法
  • 我仍然不确定您的问题。你能改写你的帖子吗?
  • 您知道如何从意图中检索extra 数据吗?只需在您调用的活动中执行 getIntent().getStringExtra("Name") 即可。
  • 是的,我知道如何从 Intent 中检索数据,但问题是“我从数据库获取一些指令到我的列表视图,现在当我选择该列表视图项目时,显示带有所选项目指令的通知它工作正常“但是,选择该通知打开一个活动,然后显示该指令,这里我面临一个问题,意味着另一个活动仅显示列表查看第一项指令。

标签: android android-activity notifications


【解决方案1】:

最后我找到了这个任务的答案。首先将这一行写入 onNewIntent(getIntent()); Oncreate() 方法。 将此代码添加到您的活动后。

protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        if(extras.containsKey("Name")){             
            setContentView(R.layout.notification);
            // extract the extra-data in the Notification
            ok_btn=(Button)findViewById(R.id.not_ok);
            String msg = extras.getString("Name");
            not_data = (TextView) findViewById(R.id.noti_txt);
            not_data.setText(msg);              
        }
    }       
    super.onNewIntent(intent);
}   

对此任务有任何疑问,请发布您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2011-10-27
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多