2012. 7. 27. 15:18

알람매니저를 이용해서 App을 만드려고 할 때 알람매니저를 이용한 예제들은 많았지만,

 다 Notification을 실행해서 Activity를 실행하는데 꽤나 애먹었던 기억이 있기에,

 행여나 저같은 사람이 있을까봐 올려봐요~





main Code

버튼하나를 만들고 버튼을 누르면 3초 뒤에 알람이 울리게 하는 코드입니다.

	package com.naddola.mytemp_alarmactivity;
	import java.util.Date;
	
	import android.app.Activity;
	import android.app.AlarmManager;
	import android.app.PendingIntent;
	import android.content.Intent;
	import android.os.Bundle;
	import android.view.View;
	import android.view.View.OnClickListener;
	import android.widget.Button;
	import android.widget.Toast;
	
	public class MyTemp_AlarmActivityActivity extends Activity {
		
		MyListener myListener;
	
		Button buttonConfirm;
	
		private Intent intent;
		private PendingIntent ServicePending;
		private AlarmManager AM;
	
		//초기화과정
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			
			myListener = new MyListener();
			
			buttonConfirm = (Button) findViewById(R.id.Confirm);
			buttonConfirm.setOnClickListener(myListener);
	
			AM = (AlarmManager) getSystemService(ALARM_SERVICE);
		}
	
		class MyListener implements OnClickListener {
	
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				
				case R.id.Confirm:
	
					//Receiver로 보내기 위한 인텐트
					intent = new Intent(getApplicationContext(), AlarmReceiver.class);
					ServicePending = PendingIntent.getBroadcast(
							MyTemp_AlarmActivityActivity.this, 0, intent, 0);
					//현재 시간보다 3초뒤에 pendingIntent를 실행
					Date t = new Date();
					t.setTime(java.lang.System.currentTimeMillis() + 3 * 1000);
					AM.set(AlarmManager.RTC_WAKEUP, t.getTime(), ServicePending);
					
					Toast.makeText(getBaseContext(), "3초뒤에 알람", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}
	}


Receiver Code

보내준 알람을 받아서 새로운 Activity를 실행시키는 코드입니다.

package com.naddola.mytemp_alarmactivity;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		
		try {
			intent = new Intent(context, MyActivity.class);
			PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
					PendingIntent.FLAG_ONE_SHOT);

			pi.send();
			
		} catch (CanceledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

AlarmActivity Code

새로운 Activity 실행

 package com.naddola.mytemp_alarmactivity;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.alarm);
	}
}

manifest.xml

Reciever와 새 Activity를 추가해줍니다.





    

    
        
            
                

                
            
        
        
        
    

Posted by 나돌라