为什么服务在华硕平板电脑中被杀死并且在三星中运行良好

时间:2017-05-31 06:12:53

标签: android service

当我的应用程序被用户杀死时,我创建了一个在后台工作的服务,它在三星平板电脑上工作正常,但是当我关闭我的应用程序时,我的华硕平板电脑被杀了! 但谷歌服务在我的华硕平板电脑上有效,这是我的代码:

public class InternetService extends Service {

      public static final int notify = 1000;  //interval between two services(Here Service run every 5 Minute)
      private Handler mHandler = new Handler();   //run on another Thread to avoid crash
      private Timer mTimer = null;    //timer handling

      @Override
      public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
      }

      @Override
      public void onCreate() {
        if (mTimer != null) // Cancel if already existed
          mTimer.cancel();
        else
          mTimer = new Timer();   //recreate new
        mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);   //Schedule task
      }

      @Override
      public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();    //For Cancel Timer
        Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
      }

      //class TimeDisplay for handling task
      class TimeDisplay extends TimerTask {
        @Override
        public void run() {
          // run on another thread
          mHandler.post(new Runnable() {
            @Override
            public void run() {

              G.AppsList.clear();
              populateDataFromServer();
              Log.i("SERVICES", "SERVICE");
            }
          });
        }
      }
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="androidmarket.ehsan.com.androidmarket">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission
        android:name="android.permission.INSTALL_PACKAGES"
        tools:ignore="ProtectedPermissions"/>

    <application
        android:name="project.utils.G"
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:supportsRtl="false"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name="project.activity.ActivityStartup">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        </activity>

        <activity android:name="project.activity.ActivityApplicationsList">

        </activity>

        <activity android:name="project.activity.ActivityApplicationDetail">
        </activity>

        <activity android:name="project.activity.ActivityInternet">

        </activity>

        <service android:name="project.service.InternetService" android:enabled="true" android:exported="true"></service>

    </application>

</manifest>

这对我来说非常重要,谢谢你的答案。

1 个答案:

答案 0 :(得分:2)

我有一个Asus Zenfone Max 3作为我的个人手机,我也在开发应用程序时使用它。华硕预先安装的Mobile Manager应用程序实际上停止了后台运行的任何内容。只需在应用程序的设置中禁用此功能即可。 Settings Screenshot