0

This method getting called again and again i.e BatteryLevelmethodon() when declared in onCreate(),onResume() and onDestroy() in main activity and the button on_off is clicked automatically.

public void BatteryLevelmethodon()
{
    final  Handler handler=new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
           Log.v(TAG,"in BatteryLevelmethod");
            on_off.performClick();
            handler.postDelayed(this, 1000);
        }
    },1000);

}

But i want that to be clicked even though the app is killed so i am calling this method BatteryLevelmethodon() in Service class in Onstart command so when we start the Service with the click of a another starts_service_button the service class executes BatteryLevelmethodon()

import android.app.Service;
import android.content.ClipData;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import static android.content.ContentValues.TAG;
import static com.light.intensity.bluetooth.le.R.id.on_off;

public class Battery_Service extends Service {
final class BatteryThread implements Runnable
{
    int serviceid;
    BatteryThread(int serviceid)
    {
        this.serviceid=serviceid;
    }

    @Override
    public void run()
    {
          synchronized (this)
        {

          try {
                wait(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            new DeviceControlActivity().BatteryLevelmethodon();
        }
          stopSelf(serviceid);

    }
}


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
       Thread thread=new Thread(new BatteryThread(startId));
        thread.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this,"Battery_service_destroy_command",Toast.LENGTH_LONG).show();
        super.onDestroy();
    }
}

But i am getting the below error log:

image

Zoe
  • 27,060
  • 21
  • 118
  • 148
vinay shetty
  • 895
  • 1
  • 9
  • 15
  • Please include the errors / stack traces as text, not images. Images can't be searched by anyone – James Z Nov 04 '17 at 15:24
  • You do not need a Handler implementation if your writing it in a service which is dedicated to your task. Services are for the background tasks that you want to achieve here. You can write your run method code directly in the on start command or you can create a thread without handler and you can start that. And start service as START_STICKY so that OS will recreate this service if killed by user or killed on Low memory. – mudit_sen Nov 05 '17 at 08:59
  • Or I have edited the last answer if you want to run this using looper and handler – mudit_sen Nov 05 '17 at 09:04
  • will try it out thank you @mudit_sen – vinay shetty Nov 06 '17 at 07:39

1 Answers1

0

Call looper.prepare() in the run() function.

public void BatteryLevelmethodon()
{
    final  Handler handler=new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
           looper.prepare()
           Log.v(TAG,"in BatteryLevelmethod");
            on_off.performClick();
            handler.postDelayed(this, 1000);
        }
    },1000);

}

If it didn't work try to put looper.prepare() in some other place ( Sorry I didn't reemeber where it actually be put ). But you can try the above code. I hope it's the rite placement.

Abdur Rahman
  • 894
  • 1
  • 11
  • 27