460
No
Run 을 통해 APP 에서 출력 확인
class MainActivity
package org.androidtown.myservice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Click(View v){
Intent intent =new Intent(getApplicationContext(), MyService.class);
intent.putExtra("command", "start");
//startActivity(); 가 아니라 서비스
startService(intent);
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.myservice.MainActivity">
<Button
android:text="서비스시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="119dp"
android:layout_marginStart="119dp"
android:layout_marginTop="104dp"
android:id="@+id/button"
android:onClick="onButton1Click"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
class MyService
package org.androidtown.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG ="MyService";
public MyService() {
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate() 호출됨.");
super.onCreate();
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy() 호출됨.");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() 호출됨");
if(intent!=null){
String command =intent.getStringExtra("command");
if(command!=null){
if(command.equals("start")){
PrintThread thread =new PrintThread();
thread.start();
}
}
}
return super.onStartCommand(intent, flags, startId);
}
class PrintThread extends Thread{
@Override
public void run() {
for(int i=0; i<100; i++){
Log.d(TAG, "#"+i+"서비스가 반복됨");
try {
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidtown.myservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
29강
30강
31강
댓글 ( 4)
댓글 남기기