user3482237
user3482237

Reputation: 27

Why is my service using a lot of battery?

This is my service:

public class reminder extends Service{

WakeLock wakeLock;
@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@Override
public void onCreate() {

    super.onCreate();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {



    Timer t=new Timer();
    final NotificationManager mgr=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //check array alarm
    final SharedPreferences settings = getSharedPreferences("arrayalarm", 0);
    final String sport = settings.getString("sport", "");
    final String[] sports=sport.split("#");
    TimerTask tt=new TimerTask() {          
        @Override
        public void run() {

        if(!(sports[0].equals("")))
            for(int i=0;i<sports.length;i++)
            {
                String[] informatins=sports[i].split(":");
                if(new Date().getHours()==Integer.parseInt(informatins[0])&&new Date().getMinutes()==Integer.parseInt(informatins[1]))
                {
                    Notification not=new Notification(R.drawable.sport,"ورزش",new Date().getTime());
                    PendingIntent pn=PendingIntent.getActivity(getApplicationContext(),0,new Intent(getApplicationContext(),MainActivity.class), 0);
                    not.setLatestEventInfo(getApplicationContext(), "ورزش", "تو الان باید ورزش کنی", pn);
                    not.flags=Notification.FLAG_AUTO_CANCEL;
                    not.vibrate=new long[] {1000l,200l,200l,500l};
                    not.sound=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.downloaddlazerdompleted);
                    mgr.notify(1304,not);
                }
            }

                }               
    };
    t.scheduleAtFixedRate(tt, 0, 60000);
    return START_STICKY;
}

@Override
public void onDestroy() {
    wakeLock.release();
    //Toast.makeText(getApplicationContext(), "god bye main human",Toast.LENGTH_SHORT).show();
    super.onDestroy();
}}

My service is reminder for sport, and when special time comes my service sends a notification.
This works well but uses a lot of battery. In setting>battery in device my app is first on the list.
How can I fix this? I also want to turn on screen when notification will be sent. How can I do that?

Upvotes: 1

Views: 150

Answers (1)

G. Blake Meike
G. Blake Meike

Reputation: 6715

Well, if I were to write an app to use up the battery as fast as possible, I think it would look a lot like this.

  • You are holding a wakelock. That means the device cannot power down. ... so it uses battery for absolutely no reason
  • You are running the vibrator every minute. The vibrator, on many phones, is a small electric motor. It is quite expensive to run it.

Have a look at the AlarmManager. You can do something similar much more cheaply.

...Including, btw, turning on the screen. To do that, do not use a notification. Instead send an Intent to your MainActivity.

Upvotes: 3

Related Questions