반응형
안녕하세요. 훈츠 입니다. 잡스케쥴 하는 방법입니다.
권한 설정 ( Manifests )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//훈스 블로그---------------------------------------------------------------------------------------------------코드//
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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">
<service
android:name=".JobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
JOB 서비스.kt 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
//훈스 블로그---------------------------------------------------------------------------------------------------코드//
class JobService : JobService(){
val TAG = "JobService"
override fun onStopJob(params: JobParameters?): Boolean {
Log.d(TAG,"job stop")
Log.d(TAGA,"job stop")
return true
}
override fun onStartJob(params: JobParameters?): Boolean {
backGroundWork(params)
return true
}
fun backGroundWork(params: JobParameters?){
Log.d(TAG,"job start")
val intent = Intent(applicationContext, MyAppService::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("command","job")
startService(intent)
jobFinished(params,true)
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
MainActivity Job 스케쥴 등록
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
//훈스 블로그---------------------------------------------------------------------------------------------------코드//
class MainActivity : AppCompatActivity() {
val TAG = "MainActivity"
private val ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 100
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 잡 시작
bt_startService.setOnClickListener {
val intent = ComponentName(this, JobService::class.java)
val info = JobInfo.Builder(123, intent)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(1000 * 60 * 15)
.build()
val jobsch = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
var resultCode = jobsch.schedule(info)
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "JOB scheduler OK ")
} else {
Log.d(TAG, "JOB scheduler Fail ")
}
}
// 잡 취소
bt_stopService.setOnClickListener{
val jobsch = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobsch.cancel(123)
Log.d(TAG, "JOB cancel ")
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
)
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE)
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'안드로이드 프로그래밍[Kotiln Code] > 안드로이드 부분 함수(권한얻기,인텐트, 데이터바인딩)' 카테고리의 다른 글
[Kotiln] 프래그먼트에서 액티비티 정보 가져오기 (0) | 2020.04.19 |
---|---|
[Kotiln] 화면 오버레이 권한 획득 (0) | 2020.04.16 |
[Kotiln] 안드로이드 죽지 않는 서비스 사용하기 (라이프 싸이클 이용) (0) | 2020.04.08 |
[Kotiln] 유저가 View 만드는 코드 (0) | 2020.04.07 |
[Kotiln] 앱 띄울때 시스템 UI를 숨기고 전체화면 표시 (0) | 2020.04.07 |