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="알람종료" /> <Button android:id="@+id/button3" 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> <receiver android:name="Antena"> <intent-filter> <action android:name="gusfree.tistory.com"/> </intent-filter> </receiver> </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); findViewById(R.id.button3).setOnClickListener(this); manager=(AlarmManager) getSystemService(ALARM_SERVICE); } @Override public void onClick(View v) { Intent intent=new Intent(); //intent.setClass(this, ToastActivity.class); //intent.putExtra("msg", "강의 시작합니다~"); PendingIntent operation= PendingIntent.getService(this, 0, intent, 0); PendingIntent.getBroadcast(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; case R.id.button3://방송보내기 Intent intent2 =new Intent(); //주파수 intent2.setAction("gusfree.tistory.com"); intent2.putExtra("msg", "강의 시작~~"); sendBroadcast(intent2); break; } } }Antena.java
package com.gusfree.alarm; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class Antena extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent intent) { String msg=intent.getStringExtra("msg"); Toast.makeText(arg0,"방송내용 "+msg,0).show(); } }
'Android > 2012.04월 강좌' 카테고리의 다른 글
10일차 PullParser (1) | 2012.05.09 |
---|---|
10일차 XMLParser (0) | 2012.05.09 |
10일차 Alarm (0) | 2012.05.09 |
9일차 ViewFlipper (0) | 2012.05.08 |
9일차 LifeCycle (0) | 2012.05.08 |