WakeLock巨大的电池消耗

时间:2015-10-13 18:41:29

标签: android wakelock android-wake-lock

各位大家好,并提前致谢,

我有一项服务,如果实际时间与给定时间(小时和分钟)匹配,则每分钟检查一次,如果是,则显示通知。问题是唤醒锁快速“喝”我的电池。我想唤醒锁的位置不正确。谁能告诉我什么是错的以及如何解决它?

谢谢!

这是我的代码......

public class ServicioRecordatorio extends Service
{
public static final long INTERVALO_NOTIFICACION = 60 * 1000;

private Handler mHandler = new Handler();

private Timer mTimer = null;

PowerManager.WakeLock wakeLock;


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


@Override
public void onCreate()
{
    if (mTimer != null)
    {
        mTimer.cancel();
    }
    else
    {
        mTimer = new Timer();
    }

    int MiWakelock = 0;

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    MiWakelock = PowerManager.PARTIAL_WAKE_LOCK;
    wakeLock = powerManager.newWakeLock(MiWakelock, "MyWakelockTag");

    wakeLock.acquire();

    mTimer.scheduleAtFixedRate(new NotificacionTimerTask(), 0, INTERVALO_NOTIFICACION);
}


class NotificacionTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        mHandler.post(new Runnable()
        {

            @Override
            public void run()
            {
                MuestraNotificacion();
            }

        });
    }


    private void MuestraNotificacion()
    {
        SharedPreferences prefs = getApplicationContext().getSharedPreferences("com.tonicc.opositest_preferences", Context.MODE_MULTI_PROCESS);

        String[] HoraMinutos = prefs.getString("horaRecordatorio", "00:00").split(":");
        int Hora = (Integer.parseInt(HoraMinutos[0]));
        int Minutos = (Integer.parseInt(HoraMinutos[1]));

        Time Ahora = new Time(Time.getCurrentTimezone());
        Ahora.setToNow();

        if(Ahora.hour == Hora && Ahora.minute == Minutos)
        {
            Boolean SONIDO_RECORDATORIO_ACTIVADO = prefs.getBoolean("sonidoRecordatorio", false);
            Boolean VIBRACION_RECORDATORIO_ACTIVADA = prefs.getBoolean("vibracionRecordatorio", false);

            Uri sonido = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fin_test);

            int notificacionID = 1;

            Intent i = new Intent(getApplicationContext(), BienvenidaActivity.class);
            i.putExtra("notificacionID", notificacionID);

            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

            CharSequence ticker = getResources().getString(R.string.ac_servicio_recordatorio_01);
            CharSequence contentTitle = getResources().getString(R.string.ac_servicio_recordatorio_02);
            CharSequence contentText = getResources().getString(R.string.ac_servicio_recordatorio_03);
            CharSequence subText = getResources().getString(R.string.ac_servicio_recordatorio_04);

            NotificationCompat.Builder noti = new NotificationCompat.Builder(getApplicationContext());

            noti.setContentIntent(pendingIntent);
            noti.setTicker(ticker);
            noti.setContentTitle(contentTitle);
            noti.setContentText(contentText);
            noti.setSubText(subText);
            noti.setSmallIcon(getResources().getIdentifier("icono_app_pequeno", "drawable", getPackageName()));
            noti.addAction(getResources().getIdentifier("icono_app_pequeno", "drawable", getPackageName()), ticker, pendingIntent);
            noti.setAutoCancel(true);

            if(SONIDO_RECORDATORIO_ACTIVADO)
            {
                noti.setSound(sonido);
            }

            if(VIBRACION_RECORDATORIO_ACTIVADA)
            {
                noti.setVibrate(new long[]{100, 250, 100, 500});
            }

            Notification n = noti.build();

            NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            nm.notify(notificacionID, n);

        }
    }
}


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

    mTimer.cancel();

    wakeLock.release();
}

}

0 个答案:

没有答案