알람기능을 어플리케이션에 넣으려고 합니다 (초보 개발자입니다)
알람 시간이 CheckBox로 뜨면 그 중에 몇 가지를 선택해서 알람을 설정하려 합니다.
일단은 코드를 아래처럼 짰는데. 알람 실행이 되질 않네요....
해결방법을 알려주세요ㅠㅠ
MainActivity.class
package com.test.alarm;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
//현재 시간 HH:mm으로 불러오기
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date = new Date();
String time = formatter.format(date);
TextView text1 = (TextView) findViewById(R.id.textView1);
text1.setText("현재 시각은 "+time+"입니다.");
//현재 시간에서 1시간 30분 후
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.HOUR, +1);
calendar1.add(Calendar.MINUTE, +30);
SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date2 = calendar1.getTime();
String time2 = formatter2.format(date2);
TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("첫 번째 렘수면 시각은 "+time2+"입니다.");
//3시간 10분 후
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.HOUR, +3);
calendar2.add(Calendar.MINUTE, +10);
SimpleDateFormat formatter3 = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date3 = calendar2.getTime();
String time3 = formatter3.format(date3);
TextView text3 = (TextView) findViewById(R.id.textView3);
text3.setText("두 번째 렘수면 시각은 "+time3+"입니다.");
//5시간 후
Calendar calendar3 = Calendar.getInstance();
calendar3.add(Calendar.HOUR, +5);
SimpleDateFormat formatter4 = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date4 = calendar3.getTime();
String time4 = formatter4.format(date4);
TextView text4 = (TextView) findViewById(R.id.textView4);
text4.setText("세 번째 렘수면 시각은 "+time4+"입니다.");
//7시간 후
Calendar calendar4 = Calendar.getInstance();
calendar4.add(Calendar.HOUR, +7);
SimpleDateFormat formatter5 = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date5 = calendar4.getTime();
String time5 = formatter5.format(date5);
TextView text5 = (TextView) findViewById(R.id.textView5);
text5.setText("네 번째 렘수면 시각은 "+time5+"입니다.");
//9시간 10분 후
Calendar calendar5 = Calendar.getInstance();
calendar5.add(Calendar.HOUR, +3);
calendar5.add(Calendar.MINUTE, +10);
SimpleDateFormat formatter6 = new SimpleDateFormat("HH:mm", Locale.KOREA);
Date date6 = calendar5.getTime();
String time6 = formatter6.format(date6);
TextView text6 = (TextView) findViewById(R.id.textView6);
text6.setText("다섯 번째 렘수면 시각은 "+time6+"입니다.");
//알람 설정
Button setAlarm = (Button) findViewById(R.id.button1);
setAlarm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final CheckBox check1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox check2 = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox check3 = (CheckBox) findViewById(R.id.checkBox3);
final CheckBox check4 = (CheckBox) findViewById(R.id.checkBox4);
final CheckBox check5 = (CheckBox) findViewById(R.id.checkBox5);
if (check1.isChecked()) {
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, +1);
calendar.add(Calendar.MINUTE, +30);
AlarmManager am1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
if (check2.isChecked()) {
Intent intent2 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender2 = PendingIntent.getBroadcast(MainActivity.this, 1, intent2, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, +3);
calendar.add(Calendar.MINUTE, +10);
AlarmManager am2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am2.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender2);
}
if (check3.isChecked()) {
Intent intent3 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender3 = PendingIntent.getBroadcast(MainActivity.this, 2, intent3, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, +5);
AlarmManager am3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am3.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender3);
}
if (check4.isChecked()) {
Intent intent4 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender4 = PendingIntent.getBroadcast(MainActivity.this, 3, intent4, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, +7);
AlarmManager am4 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am4.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender4);
}
if (check5.isChecked()) {
Intent intent5 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender5 = PendingIntent.getBroadcast(MainActivity.this, 4, intent5, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, +9);
calendar.add(Calendar.MINUTE, +10);
AlarmManager am5 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am5.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender5);
}
}
});
}
}
AlarmReceiver.class
package com.test.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "알람", Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.alarm" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> <receiver android:name=".AlarmReceiver" android:process=":remote"/> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </application>
</manifest>