I need to create alarms in android
given the time, from so much research try to do it, I would like to be guided or give me an idea that I am doing wrong since I am noob (next to many) and the app does not generate me to the notification in the android status bar, here is my code
Alert Selector.java fragment responsible for setting the alarm
package company.viral.organizadorjec.FragmentMenu.Perfil;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import company.viral.organizadorjec.ActivitysPrincipales.MainActivity;
import company.viral.organizadorjec.Clases.AlarmaNotificacionReciver;
import company.viral.organizadorjec.R;
public class SelectordeAlarmas extends Fragment {
private PendingIntent pendingIntent;
private Intent myintent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//creamos la vista del fragment
View view = inflater.inflate(R.layout.selector_alarma, container, false);
final AlarmManager alarma;
//cramos las variables del layout
Button acepalarm = (Button) view.findViewById(R.id.btnaceptaralarma);
Button boralarm = (Button) view.findViewById(R.id.btnborraralarma);
Button cancelar = (Button) view.findViewById(R.id.btncancelaralarma);
//los TV
final TextView alarmview = (TextView)view.findViewById(R.id.avisoalarm);
//alarma
final TimePicker timePicker = (TimePicker)view.findViewById(R.id.timePicker);
//instanciamos la alarma
alarma=(AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
//necesitamos un calendar para la seleccion de tiempo
final Calendar calendar=Calendar.getInstance();
//implementar el metodo de aceptar alarma y cambiar aviso
acepalarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mejorhora=String.valueOf(timePicker.getCurrentHour());
String mejorminuto=String.valueOf(timePicker.getCurrentMinute());
if (timePicker.getCurrentHour()<10){
mejorhora=String.valueOf("0"+timePicker.getCurrentHour());
}
if (timePicker.getCurrentMinute()<10){
mejorminuto=String.valueOf("0"+timePicker.getCurrentMinute());
}
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
myintent = new Intent(getContext(), AlarmaNotificacionReciver.class);
pendingIntent = PendingIntent.getBroadcast(getContext(),0,myintent,0);
alarma.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
alarmview.setText("La alarma sonara a las "+mejorhora+":"+mejorminuto);
}
});
boralarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alarmview.setText("Alarma no establecida.");
}
});
//implementamos el motodo del boton de cerrar sesion
cancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//establecemos un mensaje de alerta para la confirmacion
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("¿Desea cerrar sesion?");
builder.setTitle("Alerta!");
builder.setPositiveButton("SI", new DialogInterface.OnClickListener() {
//de ser positivo vamos al inicio y cerramo
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i= new Intent(getContext(), MainActivity.class);
startActivity(i);
getActivity().finish();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
});
return view;
}
}
selector_alarma.xml xml corresponding to the fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="company.viral.organizadorjec.FragmentMenu.Perfil.SelectordeAlarmas">>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:text="Seleccione su alarma."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="center"
android:textSize="24sp"
tools:textColor="@android:color/holo_blue_dark" />
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/timePicker" />
<TextView
android:text="Alarma no establecida."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/avisoalarm"
android:gravity="center"
android:textSize="24sp"
tools:textColor="@android:color/holo_blue_dark" />
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Aceptar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@android:style/Widget.Button"
android:id="@+id/btnaceptaralarma"
android:onClick="onClickAceptarR" />
<Button
android:text="Borrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@android:style/Widget.Button"
android:id="@+id/btnborraralarma"
android:onClick="onClickAceptarR" />
<Button
android:text="Cancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btncancelaralarma"
android:layout_weight="1"
android:onClick="onClickRegresar"
style="@android:style/Widget.Button" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
AlarmNotificationReciver.class class responsible for organizing the notification
package company.viral.organizadorjec.Clases;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import company.viral.organizadorjec.R;
/**
* Created by Genesis on 29/01/2017.
*/
public class AlarmaNotificacionReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Recordatorio")
.setContentText("Recuerda tienes trabajo")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("info");
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
}
}
At this point I do not know why when you select a time in the fragment
it does not make the notification or the corresponding alarm, there is data capture because the TextView is loaded with the previous string
saying "the alarm will sound at 02:39" (right now in my case it is early morning) and after that change in textview
it does not sound, does not generate notification, naaaada ...