android 检查系统服务,Android: Check if activity is destroyed by a system from service

本文探讨了一个服务如何监听服务器事件,并根据活动状态采取不同操作。当服务接收到事件时,如果活动未被杀死,它会向本地广播接收器发送结果并更新UI。如果活动被系统杀死,服务将重新创建活动并传递数据。关键在于识别活动是否已被销毁,以便正确处理数据和界面更新。
摘要由CSDN通过智能技术生成

I have a service listening to some events from server.

A service has START_STICKY flag that makes him restart when it's killed by OS.

When service receive an event i have two scenarios.

First, if activity isn't killed i need to send result to local broadcast receiver and update UI.

Second, if it's killed by OS i want to recreate it and send data in bundle.

But i don't know how to recognize that android killed my activity.

onDestroy activity event doesn't come in this situation.

@Override

public void onComplete(CurrentOrdersResponse response) {

if (response == null) {

return;

}

boolean isActivityDestroyed = mPreferences.getBoolean(MainActivity.IS_MAIN_ACTIVITY_DESTROYED_PREF_KEY, false);

if (!isActivityDestroyed)

sendResult(response.getResJSONStr(), CURRENT_ORDERS_ACTION);

else {

Intent intent = new Intent(this, MainActivity.class);

intent.setAction(Intent.ACTION_VIEW);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtras(extras);

startActivity(intent);

}

int resCode = response.getResCode();

Log.i(LOG_TAG, "Service resCode" + " " + resCode);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值