How to use non-static "getSystemService"

Asked

Viewed 99 times

1

I am making a class that repeats the notifications but I cannot import the Prompt class.Notification whenever I leave the Static Notification Class of the error in getSystemService in the following line

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

And whenever I leave it not Static the error alarm class on the following line

ProntoAguacasa.Notificacao a  = new ProntoAguacasa.Notificacao();

I’ll leave the code below with the error in getSystemService

public class Notificacao {
    public void GerarNotificacao(Context ctx, int dataInMillis, int hora, int minuto) {
        ArrayList<RespostasAguaCasa> pessoas = new Read().getLista();
        RespostasAguaCasa a = pessoas.get(0);
        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();
        calSet.setTimeInMillis(dataInMillis);
        calSet.set(Calendar.HOUR_OF_DAY, a.getAcordarhora());
        calSet.set(Calendar.MINUTE, a.getAcordarminu());
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);
        setAlarm(ctx, calSet);
    }

    private void setAlarm(Context ctx, Calendar targetCall)
    {
        Toast.makeText(ctx, "Alarm is set at" + targetCall.getTime(),
                Toast.LENGTH_LONG).show();
        Intent intent = new Intent(ctx, PrimeiraNotiAguaCasa.class);

        PendingIntent pendingintent = PendingIntent.getBroadcast(ctx, 1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCall.getTimeInMillis(), pendingintent);
    }
}

Alarm class

    public class PrimeiraNotiAguaCasa extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {


    List<String> opcoes1 = Arrays.asList("Chuveiro", "Luz", "Fogo", "Naruto", "Chora");
    String selecionada1 = opcoes1.get(new Random().nextInt(opcoes1.size()));
    NotificationCompat.Builder builder1 = new NotificationCompat.Builder(arg0)
            .setSmallIcon(R.drawable.proximo)
            .setContentTitle("Hora do banho ")
            .setContentText(selecionada1)
            .setAutoCancel(true);
    NotificationManager notificationManager1 = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);


    Calendar calHoje = Calendar.getInstance();
    calHoje.setTimeInMillis(System.currentTimeMillis());
    calHoje.add(Calendar.DAY_OF_MONTH, 1);
   ProntoAguacasa.Notificacao a  = new ProntoAguacasa.Notificacao();
    a.GerarNotificacao(arg0, (int) calHoje.getTimeInMillis(), 
       EscolhaAguaCasa.hourx, EscolhaAguaCasa.minutex);


}

1 answer

1


The method getSystemService() is a class method Context, so you should put ctx.getSystemService()

private void setAlarm(Context ctx, Calendar targetCall)
{
    Toast.makeText(ctx, "Alarm is set at" + targetCall.getTime(),
            Toast.LENGTH_LONG).show();
    Intent intent = new Intent(ctx, PrimeiraNotiAguaCasa.class);

    PendingIntent pendingintent = PendingIntent.getBroadcast(ctx, 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCall.getTimeInMillis(), pendingintent);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.