トップ 一覧 置換 検索 ヘルプ RSS ログイン

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

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!起動しているサービスにバインドする
!BindServiceTest.java
{{code Java,
  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
{{code Java,
  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
{{code Java,
  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>
  

{{category2 プログラミング言語,OS,Android}}
{{category2 プログラミング言語,Java,Android}}