ともちゃんのアプリ開発日記

組込みC言語プログラマだったともちゃんが、四苦八苦しながら、AndroidのJAVA/Kotlin、iOSのSwiftUIを習得して行きます。ともちゃんの備忘録も兼ねています。

端末起動時の時間のかかる処理

端末起動時に処理を動かすには、

android.intent.action.BOOT_COMPLETED

をキャッチしてやります。

public class BootBroadcastReceiver extends BroadcastReceiver {

Context context;

@Override
public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

DataCenter dataCenter = new DataCenter();
dataCenter.setContextNotificationScheduleOnBoot(context);

Intent notificationScheduleOnBoot = new Intent(context, NotificationScheduleOnBoot.class);
context.startService(notificationScheduleOnBoot);

}

}

}

 

 ただ、BroadcastReceiverの処理は、短い処理でないといけないので、処理が長くなる時は、IntentServiceを使用します。

public class NotificationScheduleOnBoot extends IntentService {

public NotificationScheduleOnBoot() {
super("NotificationScheduleOnBoot");
}

@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {

// 長くかかる処理

}
}

}