Android BroadcastReceiver、ContentProvider 和 Activity 之间的数据流?

发布于 2024-07-14 21:17:50 字数 1092 浏览 9 评论 0原文

我开发了一个接收广播然后 启动 Activity,其中 Activity 查询 ContentProvider 它实时从 DNS 中提取信息。

我希望能够对其进行洗牌,以便不要执行:

BroadcastReceiver.onReceive() {
  Intent intent = new Intent(...);
  intent.setData(...); // set a single String data
  context.startActivity(intent);
}

Activity.onCreate() {
  String value = intent.getData();  // get the String data
  Cursor = ContentProvider.query(search);
  ...
  setContentView(...);
}

它会执行:

BroadcastReceiver.onReceive() {
  Cursor = ContentProvider.query(...);
  if (cursor != null) {
     Intent intent = new Intent(...);
     // how do I pass the cursor?
     getContext().startActivity(intent);
  }
}

Activity.onCreate() {
  // how do I retrieve the cursor?
  setContentView(...);
}

即如果 query() 返回没有数据,我想错过启动 Activity,并允许广播消息正常失败。

如果 query() 确实返回数据,我希望将该 Cursor 提供给 Activity,这样我就不用再去查询数据了。

反过来,Activity 有自己的 UI,用户需要对其做出响应。

这可能吗?

I've developed an application that receives a Broadcast and then
launches an Activity, where that Activity queries a ContentProvider
which pulls information out of the DNS in real-time.

I'd like to be able to shuffle this so that instead of going:

BroadcastReceiver.onReceive() {
  Intent intent = new Intent(...);
  intent.setData(...); // set a single String data
  context.startActivity(intent);
}

Activity.onCreate() {
  String value = intent.getData();  // get the String data
  Cursor = ContentProvider.query(search);
  ...
  setContentView(...);
}

it goes:

BroadcastReceiver.onReceive() {
  Cursor = ContentProvider.query(...);
  if (cursor != null) {
     Intent intent = new Intent(...);
     // how do I pass the cursor?
     getContext().startActivity(intent);
  }
}

Activity.onCreate() {
  // how do I retrieve the cursor?
  setContentView(...);
}

i.e. if the query() returns no data I want to miss out launching the
Activity, and allow the Broadcast message to fall through as normal.

If the query() does return data, I want that Cursor to be supplied to
the Activity, so that I don't have to go and query for the data again.

In turn, the Activity has its own UI which the user needs to respond to.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-07-21 21:17:50

你想要的有点困难,对我来说,效率相当低。 我建议您使用第一种选择,但是当您在活动中加载游标时,检查是否没有数据,然后退出活动。

BroadcastReceiver.onReceive() {
  Intent intent = new Intent(...);
  intent.setData(...); // set a single String data
  context.startActivity(intent);
}

Activity.onCreate() {
  String value = intent.getData();  // get the String data
  Cursor = ContentProvider.query(search);

  if(cursor.isEmpty() ...){
    finish();
    return;
  }
  ...
  setContentView(...);
}

这将具有完全相同的效果,光标只会加载一次,并且仅当光标中存在某些内容时才会显示活动。 唯一的额外开销是无论如何都会触发意图,但这并不完全是一种负担:)

请注意,不会有任何闪烁或任何东西,Android 会处理在 onCreate() 中调用 finish 的情况(我相信 onStart 和onResume 也是如此),这样用户就永远不知道它发生了。

What you want is somewhat difficult and to me, rather inefficient. I would propose that you use the first alternative, but when you load the Cursor in the activity, check if there is no data, and then exit the activity.

BroadcastReceiver.onReceive() {
  Intent intent = new Intent(...);
  intent.setData(...); // set a single String data
  context.startActivity(intent);
}

Activity.onCreate() {
  String value = intent.getData();  // get the String data
  Cursor = ContentProvider.query(search);

  if(cursor.isEmpty() ...){
    finish();
    return;
  }
  ...
  setContentView(...);
}

This will have the exact same effect, the cursor will only be loaded once, and the activity will only be displayed if something exists in the cursor. The only extra overhead is that the intent is fired no matter what, but that's not exactly taxing :)

Note that there won't be any flicker or anything either, Android handles the case of calling finish in onCreate() (I believe onStart and onResume as well) so that the user never knows it happened.

南街女流氓 2024-07-21 21:17:50

您需要找到或创建一个可序列化或可解析的游标(然后使用intent.setExtra())。 或者也许可以将所有数据作为一个包读取并将其传递给活动?

You'll need to find or make a Cursor that's Serializable or Parcelable (and then use intent.setExtra()). Or maybe it's possible to instead read all the data in as a parcel and pass that on to the Activity?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文