从android中的点击通知中获取数据

时间:2015-12-04 17:37:04

标签: android android-pendingintent android-bundle

嘿伙计们,我需要帮助来解决如何从使用广播接收器设置的待定意图中获取数据。我想要发生的是在点击通知时获取id的数据,这将是我的活动所需要的。

这是我如何制作演员

public class AlertReceiver extends BroadcastReceiver {

    private int id;
    // Called when a broadcast is made targeting this class
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        String time = bundle.getString("time");
        id = bundle.getInt("id");

        createNotification(context, title, time, "Pharoah Reminder");
    }

    public void createNotification(Context context, String msg, String msgText,  String msgAlert){
        Intent reminderActivity =  new Intent(context, ReminderPreviewActivity.class);
        reminderActivity.putExtra("id", id);

        PendingIntent notificationIntent = PendingIntent.getActivity(context, id,
                reminderActivity, PendingIntent.FLAG_UPDATE_CURRENT );

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText)
                .setContentIntent(notificationIntent);

        mBuilder.setContentIntent(notificationIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(id, mBuilder.build());
    }
}

但是当我尝试从通知中打开我的活动时,它总是为空。

在这里我是如何得到它的。

public class ReminderPreviewActivity extends AppCompatActivity {

    private Toolbar mToolBar;

    private TextView titleTextView;
    private TextView descTextView;
    private TextView timeTextView;
    private TextView dateTextView;

    private String title;
    private String desc;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_preview);

        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);



        titleTextView   = (TextView) findViewById(R.id.titleTextView);
        descTextView    = (TextView) findViewById(R.id.descTextView);
        timeTextView    = (TextView) findViewById(R.id.timeTextView);
        dateTextView    = (TextView) findViewById(R.id.dateTextView);

        Intent extras = getIntent();

        if(extras.getStringExtra("title") != null){
            setContentFromExtras(extras);
        }else{
            setContentFromDB(extras);
        }
    }

    private void setContentFromExtras(Intent extras){
        title   = extras.getStringExtra("title");
        desc    = extras.getStringExtra("desc");
        date    = extras.getStringExtra("date");

        String[] dateDB = date.split(" ");

        titleTextView.setText(title);
        descTextView.setText(desc);
        timeTextView.setText(formatTime(dateDB[1])+" "+dateDB[2]);
        dateTextView.setText(formatDate(dateDB[0]));
    }

    public void setContentFromDB(Intent extras){
        String id = extras.getStringExtra("id");

        int reminderID = Integer.parseInt(id);

        titleTextView.setText(reminderID);
    }

我需要id从数据库中检索数据。当我关闭应用程序时会发生同样的事情。

1 个答案:

答案 0 :(得分:1)

AlertReceiver中,您已宣布

private int id;

并在

中使用此int
reminderActivity.putExtra("id", id);

因此,您还需要在int方法中将其作为setContentFromDB()

int reminderID = extras.getIntExtra("id", someInt);
titleTextView.setText("" + reminderID);

其中'someInt'应为int值,通常从不使用,如果在您的情况下有意义则为默认值。

您从getStringExtra("id")获得空值,因为如果找不到带有“id”键的String,那么这是返回值。

如果您将int值与TextView.setText()一起使用,则会将其解释为字符串资源。我认为在你的情况下('id'用于数据库)这是不好的。