티스토리 뷰
728x90
startService 구현
1. Service 구현
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PhService extends Service | |
{ | |
MediaPlayer mMediaPlayer; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mMediaPlayer = MediaPlayer.create(this, R.raw.sample); | |
mMediaPlayer.setLooping(false); | |
} | |
@Override | |
public int onStartCommand(Intent a_intent, int a_flags, int a_startId) { | |
final int result = super.onStartCommand(a_intent, a_flags, a_startId); | |
mMediaPlayer.start(); | |
return result; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mMediaPlayer.stop(); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
} |
2. Manifest 등록
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.parkho.servicesample"> | |
<application | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".PhMainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<service android:name=".PhService" /> | |
</application> | |
</manifest> |
3. Layout 생성
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<Button | |
android:id="@+id/btn_start" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="24.00dp" | |
android:text="@string/start_service" /> | |
<Button | |
android:id="@+id/btn_stop" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="24.00dp" | |
android:text="@string/stop_service" /> | |
</LinearLayout> |
4. MainActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PhMainActivity extends AppCompatActivity | |
{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// 서비스 시작 | |
Button btnStart = findViewById(R.id.btn_start); | |
btnStart.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), PhService.class); | |
startService(intent); | |
} | |
}); | |
// 서비스 종료 | |
Button btnStop = findViewById(R.id.btn_stop); | |
btnStop.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(), PhService.class); | |
stopService(intent); | |
} | |
}); | |
} | |
} |
위 예제는 하단 link 에서 확인할 수 있다.
https://github.com/parkho79/ServiceSample
728x90
'Android > Service' 카테고리의 다른 글
[Android O] Not allowed to start service Intent (2) | 2018.10.19 |
---|---|
Service (4) - bindService (0) | 2018.10.16 |
Service (3) - bound service (0) | 2018.10.10 |
Service (1) (1) | 2018.08.21 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- registerForContextMenu
- Android Service
- task
- android task
- 안드로이드 서비스
- android intent
- BroadcastReceiver
- StartService
- 무료 채굴
- onCreateContextMenu
- 앱테크
- 앱테크 추천
- WEMIX
- task 생성
- p2e
- 채굴앱
- RoomDatabase
- bindservice
- 리워드 어플
- M2E
- StringUtils
- Intent
- 리워드앱
- mPANDO
- android activity flag
- notifyDataSetChanged
- android flag activity
- 무료채굴
- onContextItemSelected
- 안드로이드 인텐트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함