如何在android app kill上停止所有服务,线程,卸载库

问题描述 投票:1回答:1

我想让用户能够选择不同语言的应用UI语言和内容。我想用服务器用用户选择的新语言重新加载新的本地化数据。

要使用新的语言设置创建应用程序,我计划:

  1. 将所选语言保存到共享首选项。
  2. 在我的两个IntentServices上调用stopService()。
  3. 用这里的答案完全杀死当前运行的应用程序:How do I programmatically "restart" an Android app?
  4. 在MyApplication启动时,首先检查共享首选项以及它们是否包含新用户语言 - 然后清除SharedPreferences,Realm数据库中的几乎所有记录。
  5. 再次启动所有服务,现在他们将转到服务器并使用新的语言优先级获取新数据。

接下来我想了解应用程序查杀的内容:

  1. 在杀死我当前正在运行的应用程序的那一刻 - 我所有正在运行的Services, Threads, ThreadExecutors, CompositeDisposable, WorkEnqueuer, Runnables, AsyncTasks, Handlers以及所有线程和(在后台工作)的东西都会在调用System.exit(0)的同时停止吗?这是否意味着当我杀死我的应用程序时,它还将完全和立即停止所有与线程相关的工作?
  2. 我使用的所有库是否都将从内存中卸载,然后在第二次正确地重新初始化?这样的图书馆如:RealmDB, Firebase, Dagger, RxJava2, Retrofit?我在项目中使用的库和服务列表如下所示。
  3. 是否会重新初始化所有静态变量?

谢谢。

这是我使用的清单和库:

<application
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".application.MyApplication"
        android:theme="@style/AppTheme"
        tools:replace="android:allowBackup">

    <activity
            android:name=".view.activity.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:launchMode="singleTask"
            android:theme="@style/Theme.AppCompat.Translucent">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <service android:name=".services.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service android:name=".services.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

    <receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

    <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.google.android.gms.analytics.AnalyticsReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
            </intent-filter>
        </receiver>


    <service
            android:name="com.google.android.gms.analytics.AnalyticsService"
            android:enabled="true"
            android:exported="false" />

        <receiver
            android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
            android:enabled="true"
            android:exported="false"/>

        <receiver
            android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
            android:enabled="true"
            android:permission="android.permission.INSTALL_PACKAGES">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

    <service
            android:name="com.google.android.gms.measurement.AppMeasurementService"
            android:enabled="true"
            android:exported="false" />

    <service
            android:name=".services.MyIntentService"
            android:enabled="true"
            android:exported="false" />
        <service
            android:name=".services.MyJobIntentService"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />

</application>

图书馆:

com.google.dagger:dagger
com.jakewharton:butterknife
io.reactivex.rxjava2:rxjava
com.github.bumptech.glide:glide
com.google.code.gson:gson
com.squareup.retrofit2:retrofit
com.facebook.android:facebook-android-sdk
com.google.android.gms:play-services-auth
com.google.firebase:firebase-core
io.realm:realm-gradle-plugin
android android-service kill kill-process android-thread
1个回答
1
投票

如果您调用system.exit(),托管您的应用程序的操作系统进程将会死亡。这将被视为“应用程序崩溃”,并可能会向用户显示一个对话框。这不是用户友好的关闭应用程序的方式,并不是在Android上执行此操作的常用方法。

在任何情况下,所有代码都将停止运行(所有线程),所有库都将被卸载,所有静态变量将被清除,因为操作系统进程将死亡,它将全部消失。

© www.soinside.com 2019 - 2024. All rights reserved.