トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

Android 起動しているサービスにバインドする

 起動しているサービスにバインドする

BindServiceTest.java

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  package test.bindservice;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.content.ServiceConnection;
  import android.content.Intent;
  import android.content.ComponentName;
  import android.content.Context;
  import android.widget.*;
  import android.view.*;
  
  public class BindServiceTest extends Activity
  {
    
    private TestService testservice = null;
    public ServiceConnection conn = new ServiceConnection(){
      @Override
      public void onServiceConnected(ComponentName className, IBinder service) {
        System.out.println("onServiceConnected2");
        testservice = ((TestService.ServiceBinder)service).getService();
      }
      
      @Override
      public void onServiceDisconnected(ComponentName className) {
        System.out.println("onServiceDisconnected2");
        testservice = null;
      }
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      initComponent();
    }
    
    private void initComponent(){
      EventProcessor ep = new EventProcessor();
      Button start_button = (Button)findViewById(R.id.start_button);
      Button stop_button = (Button)findViewById(R.id.stop_button);
      Button bind_button = (Button)findViewById(R.id.bind_button);
      Button unbind_button = (Button)findViewById(R.id.unbind_button);
      start_button.setOnClickListener(ep);
      stop_button.setOnClickListener(ep);
      bind_button.setOnClickListener(ep);
      unbind_button.setOnClickListener(ep);
    }
    
    
    private class EventProcessor implements View.OnClickListener {
      public void onClick(View v){
        if ("start_button".equals(v.getTag())){
          System.out.println(v.getTag());
          Intent i = new Intent(BindServiceTest.this, TestService.class);
          BindServiceTest.this.startService(i);
        } else if ("stop_button".equals(v.getTag())){
          System.out.println(v.getTag());
          Intent i = new Intent(BindServiceTest.this, TestService.class);
          BindServiceTest.this.stopService(i);
        } else if ("bind_button".equals(v.getTag())){
          System.out.println(v.getTag());
          Intent i = new Intent(BindServiceTest.this, TestService.class);
          BindServiceTest.this.bindService(i,conn, Context.BIND_AUTO_CREATE);
        } else if ("unbind_button".equals(v.getTag())){
          System.out.println(v.getTag());
          // Intent i = new Intent(BindServiceTest.this, TestService.class);
          BindServiceTest.this.unbindService(conn);
        }
        
      }
    }
  }

Receiver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  package test.bindservice;
  
  import android.content.*;
  import android.os.IBinder;
  import android.content.ServiceConnection;
  import android.content.Intent;
  import android.content.BroadcastReceiver;
  
  public class Receiver extends BroadcastReceiver  
  {
    
    @Override  
    public void onReceive(Context context, Intent intent) {
      System.out.println("on Receive");
      // Intent i = new Intent(context, app9.class); 
      // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      // context.startActivity(i);
      Intent i = new Intent(context, TestService.class);
      context.startService(i);
    }
    
  }

TestService.java

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
44
45
46
  package test.bindservice;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.os.Binder;
  import android.widget.Toast;
  import android.content.ServiceConnection;
  
  public class TestService extends Service  
  {
    private IBinder binder = new ServiceBinder();
    public class ServiceBinder extends Binder {
      TestService getService(){
        return TestService.this;
      }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
      System.out.println("onBind");
      return binder;
    }
    
    @Override
    public void onCreate() {
      super.onCreate();
      // Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
      System.out.println("Service Created");
    }
    
    @Override
    public void onDestroy() {
      super.onDestroy();
      // Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
      System.out.println("Service Destroyed");
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      // Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      System.out.println("Service Started");
    }
    
  }

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="test.bindservice"
   android:versionCode="1"
   android:versionName="1.0">
   <application android:label="@string/app_name">
     <activity android:name="BindServiceTest"
       android:label="@string/app_name">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
     </activity>
     
     <service android:name="test.bindservice.TestService" />
     <receiver android:name="test.bindservice.Receiver">
       <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED"/>
         <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
     </receiver>
     
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
     
   </application>
 </manifest> 

res/layout/main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <Button  
   android:id="@+id/start_button"
   android:tag="start_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Start Serviece"
   />
   <Button  
   android:id="@+id/stop_button"
   android:tag="stop_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Stop Serviece"
   />
   <Button  
   android:id="@+id/bind_button"
   android:tag="bind_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Bind Serviece"
   />
   <Button  
   android:id="@+id/unbind_button"
   android:tag="unbind_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Unbind Serviece"
   />
 </LinearLayout>
 

[カテゴリ: プログラミング言語 > Java > Android]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2011年10月27日 21時30分07秒