main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="알람시작" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="알람종료" />

</LinearLayout>
manifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gusfree.alarm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AlarmManagerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="ToastActivity"></service>
    </application>

</manifest>
AlarmManagerActivity.java
package com.gusfree.alarm;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;

public class AlarmManagerActivity extends Activity implements OnClickListener{
	
	AlarmManager manager;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        
        manager=(AlarmManager) getSystemService(ALARM_SERVICE);
    }

	@Override
	public void onClick(View v) {
		Intent intent=new Intent();
		intent.setClass(this, ToastActivity.class);
		
		PendingIntent operation=
				PendingIntent.getService(this, 0, intent, 0);
		
		switch(v.getId()){
		case R.id.button1:
			//알람 매니저 언제 어떤 동작을 실행해라
			long now=SystemClock.elapsedRealtime();
			//(int type,long triggerAtTime,long interval,PendingIntent operation)
			manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
					now+5000, 5000, operation);
			break;
		
		case R.id.button2:
			manager.cancel(operation);
			break;
		}
	}
}
package com.gusfree.alarm;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class ToastActivity extends Service{

	
	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		Toast.makeText(getApplicationContext(), "알람실행", 0).show();
		super.onStart(intent, startId);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}
}




'Android > 2012.04월 강좌' 카테고리의 다른 글

10일차 XMLParser  (0) 2012.05.09
10일차 BroadcastReceiver  (0) 2012.05.09
9일차 ViewFlipper  (0) 2012.05.08
9일차 LifeCycle  (0) 2012.05.08
9일차 SQLite 연동하기(naver 메모장)  (0) 2012.05.08

+ Recent posts