“如何修复Real Device上的'Service Destroy'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了“如何修复Real Device上的'Service Destroy'相关的知识,希望对你有一定的参考价值。

我试图用jsoup获取一些值。我必须每10秒获取一次值,因为它们每10秒刷新一次值。我的模拟器没有任何问题但是我的真实设备每10或20分钟后,在'onDestroy'中给我一个祝酒词。

我搜索修复此问题Android app working on emulator but not on real device但是它不起作用。

我的服务代码:

 @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;
        }
    }

我的活动:

@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();



                    }


    }

我们hml:

<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"/>

以上是关于“如何修复Real Device上的'Service Destroy'的主要内容,如果未能解决你的问题,请参考以下文章