我已经开发了一个接收广播然后启动的应用程序 Activity, ,在哪里 Activity 查询a 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。

这可能吗?

有帮助吗?

解决方案

您想要的东西对我来说有些困难,而且效率低下。我建议您使用第一个替代方案,但是当您将光标加载到活动中时,请检查是否没有数据,然后退出活动。

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()中呼叫完成的情况(我也相信onstart和onResume),以便用户永远不知道它发生了。

其他提示

您需要查找或制造可序列化或包裹的光标(然后使用Intent.setExtra())。或者,也许可以将所有数据读成一个包裹并将其传递给活动?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top