【问题标题】:Android: How to tell when service is completeAndroid:如何判断服务何时完成
【发布时间】:2011-04-19 20:27:37
【问题描述】:

我有一个包含按钮的活动类。当我单击该按钮时,它会启动一项服务,该服务将在单独的线程中填充我的 SQLite 数据库。服务完成后,我希望我的活动类中的文本视图显示数据库中的新值。

我已正确设置所有内容,并且我的数据库正在存储正确的信息。我唯一仍然困惑的是如何告诉我的活动课程服务已完成?

【问题讨论】:

    标签: android service android-activity broadcastreceiver android-pendingintent


    【解决方案1】:

    您可以在服务完成时发送广播。

    在您的活动 onResume

    IntentFilter intentFilter = ; //Look up how to make an intent filter with an action as specified in the below service code
    receiver =new BroadcastReceiver();
    this.registerReceiver(receiver, intentFilter);
    

    在你的活动中暂停

    this.unregisterReceiver(receiver);
    

    为您服务。

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("com.blablablaablla.ACTION_REFRESH");
    this.sendBroadcast(broadcastIntent);
    

    【讨论】:

    • 如何将广播从服务发送到活动?
    【解决方案2】:

    使用您自己的操作名称“mike.intent.DB_POPULATE_DONE”或其他名称创建一个 Intent...它只是一个字符串,如果您愿意,可以在 onNewIntent() 中检查。

    通过调用 Context.startActivity() 从 Service 发送 Intent。

    要接收 Intent,请执行 Activity.onNewIntent()

    【讨论】:

    • 我想我不想再次调用 startActivity()。我只想让我的控件使用数据库中的新值进行刷新。
    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多