繁体   English   中英

使应用保持运行状态,以接收来自城市飞艇的推动

[英]Keep app in running state to receive push from urban airship

我的应用程序使用urban Airship进行推送通知。

当用户"force" closes应用程序(通过在多任务窗口或设置中将其滑开)时,问题开始。 当推送到达时,它到达城市飞艇的接收方,并且正在广播我的应用程序使用的意图。 因此,如果该应用被强行关闭,那么我的接收器将不会被激活,我也无法接收广播。

我知道Intent.FLAG_INCLUDE_STOPPED_PACKAGES但是我不能使用它,因为Urban正在进行广播并将其保存在jar文件中。

解决的办法可能是,即使用户关闭了我,我的应用程序始终保持运行状态。 我怎样才能做到这一点? 我看到What's app正在这样做。 还有其他解决方案吗?

PS:我知道android的构建方式是当用户强制关闭某个应用程序时,他“想要”该应用程序以停用它的接收器,但对于推送通知而言,它并不相同,推送通知仍会通过。 我只希望我的应用程序“处于活动状态”,这样我才能收到推送。 谢谢!

当应用程序关闭时,服务仍处于活动状态,请尝试通过仅在服务中调用service.stopSelf()时才停止的服务来接收广播

您将希望您的服务返回START_STICKY标志

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleCommand(intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

为了在重新启动过程中持续存在,您必须创建并注册一个扩展了BroadcastReceiver的接收器,以在onReceive中启动您的服务。

在清单中添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

注册接收者:

<receiver
    android:name=".MyBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="MyBootReceiver" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <!-- Catch HTC FastBoot -->
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM