简体   繁体   English

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

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

My app uses urban Airship for its push notification. 我的应用程序使用urban Airship进行推送通知。

The problem starts when a user "force" closes the app (by sliding it away in the multitasking window or in the settings). 当用户"force" closes应用程序(通过在多任务窗口或设置中将其滑开)时,问题开始。 When a push arrives, it gets to urban airship's receiver and they are broadcasting an intent for my app to use. 当推送到达时,它到达城市飞艇的接收方,并且正在广播我的应用程序使用的意图。 So, if the app is forced closed then my receiver will not be activated and I can't receive the broadcast. 因此,如果该应用被强行关闭,那么我的接收器将不会被激活,我也无法接收广播。

I know about Intent.FLAG_INCLUDE_STOPPED_PACKAGES but I cant use it because urban is doing the broadcast and its in a jar file. 我知道Intent.FLAG_INCLUDE_STOPPED_PACKAGES但是我不能使用它,因为Urban正在进行广播并将其保存在jar文件中。

A fix could be that I keep my app in a running state all time, even if the user closes it. 解决的办法可能是,即使用户关闭了我,我的应用程序始终保持运行状态。 How can I do that? 我怎样才能做到这一点? I see that What's app is doing that. 我看到What's app正在这样做。 Is there another solution? 还有其他解决方案吗?

PS I know that android is build in a way that when a user force closes an app he "wants" it to deactivate it's receivers, but it's not the same for push notification, push notification will still get through. PS:我知道android的构建方式是当用户强制关闭某个应用程序时,他“想要”该应用程序以停用它的接收器,但对于推送通知而言,它并不相同,推送通知仍会通过。 I only want my app to "live" so I can receive the push. 我只希望我的应用程序“处于活动状态”,这样我才能收到推送。 Thanks! 谢谢!

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

You'll want your service to return the START_STICKY flag 您将希望您的服务返回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;
}

And to persist across reboots you have to create and register a receiver that extends BroadcastReceiver that starts your service in it's onReceive. 为了在重新启动过程中持续存在,您必须创建并注册一个扩展了BroadcastReceiver的接收器,以在onReceive中启动您的服务。

In your manifest add Permission: 在清单中添加权限:

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

Register Receiver: 注册接收者:

<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