2

我正在开发一个基于计时器的应用程序。无论应用程序是在前台还是后台,计时器都会在其中一项活动中连续运行。使用单独的计时器每隔一秒编写一个单独的服务来记录 GPS。活动计时器负责根据特定条件启动和停止服务。我扩展了 Application 类并覆盖了 onCreate 和 onTerminate 事件。

public class MyApplication extends Application.

和 AndroidManifest.xml

   <application
        android:allowBackup="true"
        android:icon="@drawable/launcher_icon"
        android:label="@string/app_name"
        android:persistent="true"
        android:theme="@style/AppTheme"
        android:name="MyApplication"
         >

服务

public class GPSLoggingService extends Service

当应用程序在后台运行时,有时在前台运行,onCreate 会执行多次,如果服务正在运行,它也会重新启动。

频繁重启应用程序和触发 MyApplication 类的 onCreate 可能是什么原因。这个问题的解决方案是什么?

请帮助我,因为我在过去 3 天都在尝试解决这个问题

这是完整的 androidmanifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ab.xy.cd"
    android:versionCode="1"
    android:versionName="2.0.5.15" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/launcher_icon"
        android:label="@string/app_name"
        android:persistent="true"
        android:theme="@style/AppTheme"
        android:name="MyApplication"
         >

        <!-- Since application is using map add following -->
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name="ab.xy.cd.ui.Splash"
            android:label="@string/app_name" 
            >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Login"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" 
            >
        </activity>
        <!--
             stateHidden is required if you dont want to show keyboard when activity starts and 
             edit text gets focus. In this case if user clicks on any edit text the on the same
             layout, the keyboard will be poped up 
        -->
        <activity
            android:name="ab.xy.cd.ui.BasicSettings"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden|adjustPan"
             >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name"
            android:theme="@style/Theme.NoBackground"
            android:hardwareAccelerated="true" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Challenges"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Slim"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden|adjustPan" 
            >

        </activity>
        <activity
            android:name="ab.xy.cd.ui.Score"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Settings"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.TextSummary"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.EventSetting"
            android:label="@string/app_name"
             >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.AboutMapAndTraffic"
            android:label="@string/app_name"
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.AboutUs"
            android:label="@string/app_name"
             >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.VersionInformation"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Language"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Search"
            android:label="@string/app_name"
             >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.TutorialFromLogin"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ui.Tutorial"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ShutdownActivity"
            android:label="@string/app_name" 
            >
        </activity>
        <activity
            android:name="ab.xy.cd.ExtraFor"
            android:label="@string/app_name"
             >
        </activity>

        <service android:name=".GPSLoggingService"
            android:process=":gpslogging"
            >
        </service>

        <receiver
            android:name="ab.xy.cd.utils.NetworkStateInformer"
            android:label="NetworkStateInformer" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
                </action>
            </intent-filter>
        </receiver>
         <receiver android:name="ab.xy.cd.utils.RushHourReceiver">
        </receiver>
        <receiver android:name="ab.xy.cd.utils.ServiceStartReceiver">
        </receiver>
        <receiver android:name="ab.xy.cd.utils.ServiceEndReceiver">
        </receiver>
    </application>

</manifest>
4

0 回答 0