“如何在真实设备上修复 'Service Destroy'

"How to fix 'Service Destroy' on Real Device

我正在尝试使用 jsoup 获取一些值。我必须每 10 秒取一次值,因为它们每 10 秒刷新一次值。我的模拟器没有任何问题,但我的真实设备每隔 10 或 20 分钟后,就会在 'onDestroy' 中给我一条吐司消息。

我搜索了解决这个问题的方法但是没用。

我的服务代码:

 @Nullable
    @Override
    public IBinder onBind(Intent ıntent) {
        return mBinder;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        Toast.makeText(this, "created", Toast.LENGTH_SHORT).show();


    }


 @Override
    public int onStartCommand(Intent intent, int flags, int startId ) {

        Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
// // TODO: 22.03.2019 Hocam buradan gönderilebilir yalnız boş değer kabul etmez.
        timer = new Timer();
        timer.schedule(new TimerTask() {  //her 60 sn de bir bildirimGonder(); metodu çağırılır.
            @Override
            public void run() {
                new getirHtml().execute();
            }

        }, 0, 5000);
   return START_STICKY;


    }



 public class getirHtml extends AsyncTask<Void,Void,Void > {
        @Override
        protected Void doInBackground(Void... voids) {

            try {

                document = Jsoup.connect(url).get();




                h1element = document.select("div[data-id=rdIntBranchDoviz]");


            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;
        }

   @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
  }


  @Override
    public void onDestroy() {

        //  timer.cancel();
        Toast.makeText(this, "The service has stopped. This message came from service class.", Toast.LENGTH_LONG).show();

        Log.i(TAG, "onDestroy: " + "worked");
    }


    public class LocalBinder extends Binder {
        public doviz_takip_servis getServerInstance() {
            return doviz_takip_servis.this;
        }
    }

我的activity:

@Override
    public void onCreate() {

 Intent doviz_takip_servis = new Intent(getApplicationContext(),doviz_takip_servis.class);
        startService(doviz_takip_servis);

    }




    @Override
    protected void onStart() {
        super.onStart();

        Intent mIntent = new Intent(this, doviz_takip_servis.class);
       bindService(mIntent, mConnection, BIND_AUTO_CREATE);

    };

    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBounded = false;
            mServer = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBounded = true;
            something.about.hatay.dviztakip.doviz_takip_servis.LocalBinder mLocalBinder = (something.about.hatay.dviztakip.doviz_takip_servis.LocalBinder)service;
            mServer = mLocalBinder.getServerInstance();
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        if(mBounded) {
            unbindService(mConnection);
            mBounded = false;
        }
    }

广播接收器

{

        @Override
        public void onReceive(final Context context, Intent ıntent) {

                Bundle extras = ıntent.getExtras();
                String data = extras.getString("key");
                String dataaa = extras.getString("dolar");


                if (data != null){

                    String[] separated = data.split("\ ");

                    amerikan_doları_alıs = separated[15]    ;
                }


     if (dataaa != null  ){
                    if (Double.parseDouble(amerikan_doları_alıs) > Double.parseDouble(dataaa)){
                      // Toast.makeText(context, "" + dataaa, Toast.LENGTH_SHORT).show();



                    }


    }

我的xml:

<service
    android:name=".doviz_takip_servis"
    android:exported="false"

    android:supportsRtl="true"

    />

<receiver android:name=".update">
    <intent-filter>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

我对我的代码的期望是模拟器。我该怎么做?

首先;感谢 CommonsWare 提供的提示。

我检查了所有文件 https://developer.android.com/about/versions/oreo/android-8.0-changes

我刚刚将我的应用程序插入白名单,如下所示:

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent();
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
        }
    }

不要忘记添加权限:

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