bindService demo

Hoto Cocoa發表於2020-12-01
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloWorld">
        <activity android:name="com.example.helloworld.TestService2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".Myservice1"/>
    </application>
</manifest>
public class TestService2 extends AppCompatActivity {
    private Myservice1.MyBinder binder;
    private ServiceConnection conn=new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("tttt","---service onServiceConnected---");
            binder=(Myservice1.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("tttt","---service onServiceDisconnected---");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horizontalscrollview);
        init1();
    }

    public void init1() {
        LinearLayout ll1 = findViewById(R.id.ll1);
        Button b = new Button(this);
        Button b1 = new Button(this);Button b2 = new Button(this);
        b.setText("button1");
        b1.setText("button2");b2.setText("button3");
        final Intent i=new Intent(TestService2.this,Myservice1.class);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("tttt", "i bind");
                bindService(i,conn, Service.BIND_AUTO_CREATE);
            }
        });
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("tttt", "i stopService//unbind");
                unbindService(conn);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"count="+binder.getCount(),
                		Toast.LENGTH_SHORT).show();
            }
        });
        ll1.addView(b);
        ll1.addView(b1);ll1.addView(b2);
    }

    @Override
    protected void onDestroy() {
        unbindService(conn);
        super.onDestroy();
    }
}
public class Myservice1 extends Service {
    private boolean quit;private volatile int count;
    private MyBinder binder=new MyBinder();
    public class MyBinder extends Binder {
        public int getCount(){return count;}
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("tttt", "onBind");
        return binder;
    }

    @Override
    public void onCreate() {
        Log.e("tttt", "onCreate");
        super.onCreate();
        new Thread(){
            @Override
            public void run() {
                while(!quit){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count=count+1;
                    Log.e("tttt", "count="+count);
                }
            }
        }.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("tttt", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.e("tttt", "onDestroy");
        quit=true;
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("tttt", "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.e("tttt", "onRebind");
        super.onRebind(intent);
    }
}

相關文章