0
Я использую кнопку переключения, чтобы установить будильник, который вызывает класс в другом пакете, чтобы добавить необходимую информацию. Существует класс службы, который будет вызван. Но уведомление не придет.Тревога не установлена
Heres мой код:
int mYear = Integer.parseInt(formattedYear);
int mMonth = Integer.parseInt(formattedMonth);
int mDay = Integer.parseInt(formattedDay) - 1; // 1 day before the exact day
int mHour = Integer.parseInt(formattedHour);
int mMin = Integer.parseInt(formattedMin);
SetAlarm sa = new SetAlarm(context, mYear, mMonth, mDay, mHour, mMin, title, body, iD);
sa.startAlarm();
public class SetAlarm {
private Context context;
private PendingIntent mAlarmSender;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMin;
private int iD;
String title;
String body;
public SetAlarm(Context context, int mYear, int mMonth, int mDay, int mHour, int mMin, String title, String body, int iD) {
this.context = context;
this.mYear = mYear;
this.mMonth = mMonth;
this.mDay = mDay;
this.mHour = mHour;
this.mMin = mMin;
this.title = title;
this.body = body;
this.iD =iD;
}
public void startAlarm(){
Intent intent = new Intent();
//Set the alarm to 10 seconds from now
Calendar c = Calendar.getInstance();
Log.d("Check", mYear+"/"+mMonth+"/"+mDay+"/"+mHour+"/"+mMin);
c.add(Calendar.YEAR, mYear);
c.add(Calendar.MONTH, mMonth);
c.add(Calendar.DAY_OF_MONTH, mDay);
c.add(Calendar.HOUR_OF_DAY, mHour);
c.add(Calendar.MINUTE, mMin);
c.add(Calendar.SECOND, 00);
// Schedule the alarm!
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
intent.setClass(context, com.example.alarm.helper.AlarmReceiver.class);
mAlarmSender = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() , mAlarmSender);
}
}
Класс Сервис:
public class AlarmReceiver extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this , MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
int icon = R.drawable.ic_notification;
String tickerText = "Reminder";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
String contentTitle = "Title";
String contentText = "Text";
notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
notificationManager.notify(123, notification);
Toast.makeText(this, "Reminder On", Toast.LENGTH_SHORT).show();
}
}
Android Manifest:
<service android:name="com.example.alarm.helper.AlarmReceiver"
android:enabled="true">
</service>
Кадр уведомления обыкновение показывать. – Ruch1234
нет, он отображается, и он отлично работает в моем приложении. –
см. Эту ссылку https://codeload.github.com/manishsri01/AndroidAlarmManagerDemo/zip/master –