1. 브로드캐스트 리시버는 반드시 새로운 java파일을 만들어 코딩해야 하나요?
MainActivity에서 onRecieve() 클래스를 밑에 붙여서 코딩하면 안되는건지 궁금합니다
2. 브로드캐스트 리시버를 이용해 전원이 켜졌을경우 노티피케이션 알림을 설정하려고 하는데요
public class Receiver extends Activity {
NotificationManager notiManager;
Vibrator vibrator;
private int notiHour,notiMin;
private long notiFlag;
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//notiHour,notiMin 불러오기--------------------------------------------------------
SQLiteDatabase db = null;
if (db == null) {
db = openOrCreateDatabase(
"sqlite_todo.db",
SQLiteDatabase.CREATE_IF_NECESSARY,
null);
}
Cursor c = db.rawQuery("SELECT * FROM notiTime ORDER BY _id ASC;", null);
c.moveToFirst();
if(c.getCount() == 0){
notiHour = 12;
notiMin = 0;
}
else{
notiHour = c.getInt(1);
notiMin = c.getInt(2);
}
c.close();
if(db != null) {
db.close();
}
//----------------------------------------------------------------------------------
//Notification Handler 설정
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, notiHour);
cal1.set(Calendar.MINUTE, notiMin);
cal1.set(Calendar.SECOND, 0);
if(cal1.getTimeInMillis() > cal2.getTimeInMillis()) //노티 타임이 지나지 않았을때
notiFlag = cal1.getTimeInMillis() - cal2.getTimeInMillis(); // 노티타임까지 남은 시간을 계산하여 notiFalg에 넣는다
else //노티 타임이 지난 이후일때
notiFlag = cal1.getTimeInMillis() - cal2.getTimeInMillis() + (24 * 60 * 60 * 1000); // 내일 노티타임까지 남은 시간을 계산하여 넣는다
handler.sendEmptyMessageDelayed(1, notiFlag);
}
}
//일정 시간 후에 알림을 발생시킬 핸들러
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Calendar cal = Calendar.getInstance();
Notification noti = new Notification(R.drawable.ic_launcher//알림창에 띄울 아이콘
, "오늘의 일정입니다 [Do.NOTE]", //간단 메세지
System.currentTimeMillis()); //도착 시간
noti.defaults = Notification.DEFAULT_SOUND;
noti.flags = Notification.FLAG_ONLY_ALERT_ONCE;
noti.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(Receiver.this, Scheduler.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingI = PendingIntent.getActivity(Receiver.this, 0, intent, 0);
noti.setLatestEventInfo(Receiver.this, "Do.NOTE", "노티 메시지 TEST" , pendingI);
//알림창 띄우기(알림이 여러개일수도 있으니 알림을 구별할 상수값, 여러개라면 상수값을 달리 줘야 한다.)
if(cal.get(Calendar.HOUR_OF_DAY) == notiHour && cal.get(Calendar.MINUTE) == notiMin){
notiManager.notify(1, noti);
//진동주기(** 퍼미션이 필요함 **)
vibrator.vibrate(1000); //1초 동안 진동
}
//사용자가 노티를 확인하지 않았을 경우를 대비, 재귀적으로 내일 노티를 바로 설정해준다.
//Notification Handler 설정
handler.sendEmptyMessageDelayed(1, 24 * 60 * 60 * 1000);
}
};
}
이런식으로 코딩했는데 알림이 안울리네요... 왜 그런걸까요?..
단순히 Activity 에 onRecieve 메소드만 붙인다고 되는 것이 아닙니다. Activity 안에 broadcast reciever 에 대한 내부 클래스를 두시고 그 안에서 onRecieve 를 재정의 하세요. register, unregister 하는 부분도 추가하셔야 합니다. 위 코드의 onRecieve 메소드는 아무 의미 없는 것입니다.




아! 추가로 질문 하나만 더 드릴게요ㅠㅠㅠㅠ
노티피케이션 이용할때요
카톡 메시지 뜨는것처럼 핸드폰 액정 꺼져있을때 액정 켜지면서 팝업창 뜨도록 하는거는
설정 어떻게 하나요???
고수님들의 조언 부탁드리겠습니다!!!!