반응형

AlarmActivity.java

 // 알람 등록

    private void setAlarm(Context context, long second){  

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

 

        Intent intent = new Intent(AlarmSetting.this, AlarmReceive.class);

        PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

         

//second​ 초후에 알람 울리게 설정(1000*실제 초 ex>5초 = 5000)

 

        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + second, pIntent);

    }

     

    // 알람 해제

    private void releaseAlarm(Context context){  

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

 

        Intent Intent = new Intent(INTENT_ACTION);

        PendingIntent pIntent = PendingIntent.getActivity(context, 0, Intent, 0);

        alarmManager.cancel(pIntent);

         

        // 주석을 풀면 먼저 실행되는 알람이 있을 경우, 제거하고

        // 새로 알람을 실행하게 된다. 상황에 따라 유용하게 사용 할 수 있다.

//      alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 3000, pIntent);

    }

 

 

AlarmReceive .java

 public class AlarmReceive extends BroadcastReceiver {

 

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Toast.makeText(context, "Alarm Received!", Toast.LENGTH_LONG).show();

 

NotificationManager notifier = (NotificationManager) context

.getSystemService(Context.NOTIFICATION_SERVICE);

 

Notification notify = new Notification(R.drawable.ic_launcher, "text", System.currentTimeMillis());

 

Intent intent2 = new Intent(context, CallResultActivity.class);

PendingIntent pender = PendingIntent.getActivity(context, 0, intent2, 0);

 

notify.setLatestEventInfo(context, "alimtitle", "hackjang", pender);

 

notify.flags |= Notification.FLAG_AUTO_CANCEL;

notify.vibrate = new long[] { 200, 200, 500, 300 };

// notify.sound=Uri.parse("file:/");

notify.number++;

 

notifier.notify(1, notify);

}

 

}

 

※여기서 중요한 것은 BroadcastReceiver  사용할때 manifast.xml에

<receiver android:name=".AlarmReceive"/> <--이것을 반드시 추가 할 것!!

 

 

 


반응형
,