반응형

 

 

작동은 아래 포스팅 처럼 동작합니다. 좀 부족함 점이 많지만 공개합니다.

 

[Android/프로그래밍] - IntentService 구현 중 2..( 안드로이드 백그라운드 서비스)

http://webnautes.tistory.com/664

 

 

MainActivity.java

  1. package com.webnautes.backgroundserviceexample;  
  2.     
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9.     
  10.     
  11. public class MainActivity extends Activity implements BackgroundResultReceiver.Receiver  {  
  12.     int count=0;  
  13.     public BackgroundResultReceiver mReceiver;  
  14.     private TextView text1;  
  15.     boolean isFinish = false;  
  16.     
  17.     
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         text1 = (TextView) findViewById(R.id.text1);  
  23.     
  24.         String str = Integer.toString(count);  
  25.         text1.setText(str );  
  26.     }  
  27.     
  28.     @Override  
  29.     protected void onRestart() {  
  30.         super.onRestart();  
  31.     
  32.         Toast.makeText(getApplicationContext(),  
  33.                 "onRestart", Toast.LENGTH_LONG).show();  
  34.     
  35.         final Intent intent = new Intent(Intent.ACTION_SYNC, nullthis, BackgroundService.class);  
  36.         stopService(intent);  
  37.     
  38.     
  39.     }  
  40.     
  41.     @Override  
  42.     public void onReceiveResult(int resultCode, Bundle resultData) {  
  43.         switch (resultCode) {  
  44.             case BackgroundService.STATUS_RUNNING:  
  45.     
  46.                 Toast.makeText(this"STATUS_RUNNING", Toast.LENGTH_LONG).show();  
  47.                 break;  
  48.     
  49.     
  50.             case BackgroundService.STATUS_FINISHED:  
  51.     
  52.                 count = resultData.getInt("back");  
  53.                 String str = Integer.toString(count);  
  54.     
  55.                 text1.setText(str);  
  56.     
  57.                 Toast.makeText(this"STATUS_FINISHED", Toast.LENGTH_LONG).show();  
  58.     
  59.                 break;  
  60.     
  61.     
  62.             case BackgroundService.STATUS_ERROR:  
  63.                 Toast.makeText(this"STATUS_ERROR", Toast.LENGTH_LONG).show();  
  64.                 break;  
  65.         }  
  66.     }  
  67.     
  68.     
  69.     @Override  
  70.     protected void onStop() {  
  71.         super.onStop();  
  72.         Toast.makeText(getApplicationContext(),  
  73.                 "onStop", Toast.LENGTH_LONG).show();  
  74.     
  75.         mReceiver = new BackgroundResultReceiver(new Handler());  
  76.         mReceiver.setReceiver(this);  
  77.     
  78.     
  79.           
  80.         final Intent intent = new Intent(Intent.ACTION_SYNC, nullthis, BackgroundService.class);  
  81.     
  82.           
  83.         intent.putExtra("count", count);  
  84.     
  85.         intent.putExtra("receiver", mReceiver);  
  86.         intent.putExtra("command""increase count");  
  87.         startService(intent);  
  88.     
  89.     }  
  90. }  

 

 

BackgroundResultReceiver.java

  1. package com.webnautes.backgroundserviceexample;  
  2.     
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.ResultReceiver;  
  6.     
  7.     
  8. public class BackgroundResultReceiver extends ResultReceiver {  
  9.     private Receiver mReceiver;  
  10.     
  11.     public BackgroundResultReceiver(Handler handler) {  
  12.         super(handler);  
  13.     }  
  14.     
  15.     public void setReceiver(Receiver receiver) {  
  16.         mReceiver = receiver;  
  17.     }  
  18.     
  19.     public interface Receiver {  
  20.         public void onReceiveResult(int resultCode, Bundle resultData);  
  21.     }  
  22.     
  23.     @Override  
  24.     protected void onReceiveResult(int resultCode, Bundle resultData) {  
  25.         if (mReceiver != null) {  
  26.             mReceiver.onReceiveResult(resultCode, resultData);  
  27.         }  
  28.     }  
  29.     
  30. }  

 

 

BackgroundService.java

  1. package com.webnautes.backgroundserviceexample;  
  2.     
  3. import android.app.IntentService;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.os.ResultReceiver;  
  7.     
  8.     
  9. public class BackgroundService extends IntentService {  
  10.     
  11.     public static final int STATUS_RUNNING = 0;  
  12.     public static final int STATUS_FINISHED = 1;  
  13.     public static final int STATUS_ERROR = 2;  
  14.     ResultReceiver receiver = null;  
  15.     int count;  
  16.     boolean isRunning = true;  
  17.     
  18.     
  19.     public BackgroundService() {  
  20.         super("BackgroundService");  
  21.     }  
  22.     
  23.     
  24.     @Override  
  25.     protected void onHandleIntent(Intent workIntent) {  
  26.         count = workIntent.getExtras().getInt("count");  
  27.         receiver = workIntent.getParcelableExtra("receiver");  
  28.     
  29.         String command = workIntent.getStringExtra("command");  
  30.     
  31.         if (command.equals("increase count")) {  
  32.             receiver.send(STATUS_RUNNING, Bundle.EMPTY);  
  33.         }  
  34.     
  35.         //1초에 한번씩 카운터 값을 증가시킨다..  
  36.         while(isRunning) {  
  37.             count = count + 1;  
  38.     
  39.             try {  
  40.                 Thread.sleep(1000);  
  41.             } catch (InterruptedException e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.      }  
  46.     
  47.     @Override  
  48.     public void onDestroy ()  
  49.     {  
  50.         //카운트  증가시키는 루프를 중단시키고  
  51.         isRunning = false;  
  52.     
  53.         //증가된 카운트 값을 리턴한다.  
  54.         Bundle b = new Bundle();  
  55.         try {  
  56.             b.putInt( "back", count);  
  57.             receiver.send(STATUS_FINISHED, b);  
  58.     
  59.         } catch(Exception e) {  
  60.             b.putString(Intent.EXTRA_TEXT, e.toString());  
  61.             receiver.send(STATUS_ERROR, b);  
  62.         }  
  63.     }  
  64.     
  65. }  

 

 

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.webnautes.backgroundserviceexample" >  
  4.     
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@drawable/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:theme="@style/AppTheme" >  
  10.         <activity  
  11.             android:name=".MainActivity"  
  12.             android:label="@string/app_name" >  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.     
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     
  20.         <service  
  21.             android:name=".BackgroundService"  
  22.             android:exported="false"/>  
  23.     
  24.     </application>  
  25.     
  26. </manifest>  
반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts