0

This question is along the lines of the one I asked for iOS, and not related to this one.

I want to know what are the mechanisms that one can use to launch an app on the Android device that has been force quit, or which hasn't been started since the device was last restarted. I imagine there is no way to launch the app in the foreground, but there should be a way to launch it in the background.

Perhaps we can send some sort of "silent remote notification" which will launch our app in order to process this notification, and which may in turn decide to set a local notification to display to the user.

I am looking for a documented 100% reliable way that is not subject to the whim of the OS, but will actually launch the app in the background, and allow it to do things.

4

2 回答 2

0

试试这个指南。基本上你把它添加到你的清单中:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    ...
    <!--
        Because android:exported is set to "false",
        the service is only available to this app.
    -->
    <service
        android:name=".RSSPullService"
        android:exported="false"/>
    ...
<application/>

然后将此块添加到您需要的任何内容中:

public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}

指南也有帮助。

于 2017-03-26T21:29:09.890 回答
0

您可以从 a 启动应用程序BroadcastReceiver以响应各种系统广播,包括设备启动、预定警报等。托管后台进程的正确组件是Service. 如果服务请求前台状态,则它们被杀死的可能性较小,这会创建一个持续的(不可关闭的)通知。

主要的警告是,BroadcastReceiver除非用户自安装或自上次强制关闭以来手动启动了应用程序的一项活动,否则将永远不会调用您。

于 2017-03-26T21:29:54.370 回答