알람 기능을 포함한 앱을 만들려고 하는데 앱을 종료해도 설정한 시간이 되면 액티비티가 뜨고 알람이 울리게 하려고 foreground를 적용했습니다.
근데 이게 올바르지 않은 적용방법인지 실행이 안되네요.
시간상으로만 거의 3일을 이거를 고치려고 하고있는데 아무리 찾아도 방법을 모르겠습니다 ㅠㅠ
어디가 틀렸는지를 모르니 아무리 검색을 해도 이해를 못하겠습니다.
현재 문제는 알람 자체는 제대로 작동하는데 앱을 종료한 상태에서 등록한 시간이 되면 notification만 잠깐 뜨고 액티비티가 등장하지 않습니다..
로그캣을 보면 에러는 전혀 없고 "Alarm"이라고 로그가 뜨는걸 봐서는 정상 작동하는거 같은데 액티비티만 안뜨니 해결할 방법을 모르겠어서 질문드립니다. ㅠㅠ
글의 두서가 없고 가독성이 떨어지는 점 죄송합니다..
// AlarmActivity.java 의 일부
private void setAlarm() {
// 알람 시간 설정
this.calendar.set(Calendar.HOUR_OF_DAY, this.timePicker.getHour());
this.calendar.set(Calendar.MINUTE, this.timePicker.getMinute());
this.calendar.set(Calendar.SECOND, 0);
// 현재일보다 이전이면 등록 실패
if (this.calendar.before(Calendar.getInstance())) {
Toast.makeText(this, "알람시간이 현재시간보다 이전일 수 없습니다.", Toast.LENGTH_LONG).show();
return;
}
// Receiver 설정
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 알람 설정
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, this.calendar.getTimeInMillis(), pendingIntent);
// Toast 보여주기 (알람 시간 표시)
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Toast.makeText(this, "Alarm : " + format.format(calendar.getTime()), Toast.LENGTH_LONG).show();
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent sIntent = new Intent(context, AlarmService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(sIntent);
} else {
context.startService(sIntent);
}
}
}
public class AlarmService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
Notification notification = builder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
//.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1, notification);
}
// 알람창 호출
Intent intent1 = new Intent(getApplicationContext(), AlarmActivity2.class);
// 새로운 TASK 를 생성해서 Activity 를 최상위로 올림
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
Log.d("AlarmService", "Alarm");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
stopSelf();
return START_NOT_STICKY;
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel() {
String channelId = "Alarm";
String channelName = getString(R.string.app_name);
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
//channel.setDescription(channelName);
channel.setSound(null, null);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
return channelId;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scheduler4">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmActivity"
android:label="AlarmActivity" />
<activity
android:name=".AlarmActivity2"
android:label="AlarmActivity" />
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" />
<service
android:name=".AlarmService"
android:enabled="true" />
</application>
</manifest>
Android 10.0 버전 사용하고있습니다.