BroadCast notification when Android device is sleeping

1

I have an activity that calls a function from a service class that creates a intent = NEW INTENT("MEU_BROADCAST") and also an AlarmManager . Within the BroadCast class I call an activity that creates a notification.

Everything working perfectly, my problem is that I want it to wake up even when the user clicks the sleep button, because it does not wake up if it is asleep, only when the user clicks the power to turn on the screen. I tried using PowerManager just that it is not working! Can anyone help me?

This function belongs to my class that does not extend from anything! is just a function that I call via an OBJECT

public void gravarAlertaDeEvento(Context ctx, int alarmId, int mHour, int mMinute, boolean repeat, Calendar calendar, String titulo, String evento, boolean som)
{
    //Intent intentAlarme = new Intent(ctx, Alarme.class);
    Intent intentAlarme = new Intent("EXECUTAR_ALARME");
    //intentAlarme.setAction("Start");
    intentAlarme.putExtra("id_evento", ""+alarmId);
    intentAlarme.putExtra("musica", som);
    intentAlarme.putExtra("titulo_evento", titulo); //Insere titulo do evento salvo
    intentAlarme.putExtra("evento", evento);    //insere info do evento salvo
    //PendingIntent enviarAlarme = PendingIntent.getActivity(ctx, alarmId, intentAlarme, 0);
    PendingIntent enviarAlarme = PendingIntent.getBroadcast(ctx, alarmId, intentAlarme, 0);

    AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    if(repeat)
    {
        am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), calendar.getTimeInMillis(), enviarAlarme);
    }else
    {
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), enviarAlarme); 
    }   
}   

My Service class:

public class GerandoNotificacao extends Service{

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@SuppressWarnings("deprecation")
public void onStart(Intent intent, int startId){
    super.onStart(intent, startId);

    try {

        String titulo = intent.getStringExtra("titulo_evento");
        String assunto = intent .getStringExtra("evento");

        NotificationManager notManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notific = new Notification(R.drawable.calendar2, "Evento", System.currentTimeMillis());

        if (intent.getBooleanExtra("musica", false))
        {
            notific.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/"+ R.raw.alarme);
        }           

        notific.defaults = Notification.DEFAULT_LIGHTS;
        notific.flags |= Notification.FLAG_INSISTENT;
        notific.flags |= Notification.FLAG_AUTO_CANCEL;

        notific.ledARGB = R.color.seaGreen;

        notific.vibrate = new long[]{ 100, 250, 100, 250, 100, 250, 100, 250, 500 };

        int idEvento = Integer.parseInt(intent.getStringExtra("id_evento")); //pegando id da notificação

        Intent activityAbrir = new Intent(this, EditarEvento.class);
        activityAbrir.putExtra("tela", "eventos");
        activityAbrir.putExtra("p_c", ""+idEvento);
        activityAbrir.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);   

        //PendingIntent para executar a Activity se o usuário selecionar a notificação
        PendingIntent pendInt = PendingIntent.getActivity(this, idEvento, activityAbrir, 0);

        notific.setLatestEventInfo(getApplicationContext(), titulo, assunto, pendInt);
        notManager.notify(idEvento, notific);       

        WakefulBroadcastReceiver.completeWakefulIntent(intent);

    } catch (Exception e) {
        //Toast.makeText(getApplicationContext(), "Alert: "+e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

}

My WakeupfullBroadcastReceived class

@Override
public void onReceive(Context c, Intent i) {

    Intent intet = new Intent(c, GerandoNotificacao.class); //Chamando a classe notificação
    intet.putExtra("id_evento", i.getStringExtra("id_evento"));      //insere id do evento (banco de dados)
    intet.putExtra("musica", i.getBooleanExtra("musica", false));
    intet.putExtra("titulo_evento", i.getStringExtra("titulo_evento")); //Insere titulo do evento salvo
    intet.putExtra("evento", i.getStringExtra("evento"));   //insere info do evento salvo

    /*try {
        this.finalize();
    } catch (Throwable e) { Log.i("Erro borad: ", e.getMessage());  }*/
    startWakefulService(c, intet);        
}

My Manifest

<receiver android:name="br.com.AlarmeEvento.Alarme" >
        <intent-filter>
            <action android:name="EXECUTAR_ALARME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    
asked by anonymous 04.06.2014 / 19:48

1 answer

0

Who has to have a Wake Lock to keep the CPU awake is the broadcast receiver and not the code that schedules the alarm next to the AlarmManager. I suggest that your broadcast receiver be a subclass of WakefulBroadcastReceiver . If alarm triggering (sound to be played, etc.) is performed within a separate service and not directly at onReceive() , the service before terminating must call WakefulBroadcastReceiver.completeWakefulIntent(intent) for Wake Lock to be released. p>     

04.06.2014 / 19:58