android Service

来源:互联网 发布:淘宝刷一个皇冠多少钱 编辑:程序博客网 时间:2024/06/08 03:21
Myservice
package com.example.halfopen.mytools;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class MyService extends Service {    private String s = "默认信息";    private boolean running = false;    private SetBinder binder = new SetBinder();    private Callback callback = null;    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        return binder;    }    @Override    public void onDestroy() {        super.onDestroy();        this.running = false;    }    @Override    public void onCreate() {        super.onCreate();        running = true;        new Thread(){            @Override            public void run() {                super.run();                int i = 0;                while(running){                    i++;                    String str = i+s;                    System.out.println(str);                    if(callback !=null){                        callback.onDataChange(str);                    }                    try {                        sleep(1000);                    }catch (InterruptedException e){                        e.printStackTrace();                    }                }            }        }.start();    }    public Callback getCallback() {        return callback;    }    public void setCallback(Callback callback) {        this.callback = callback;    }    class SetBinder extends Binder{        public void setRunning(boolean flag){            running = flag;        }        public MyService getService(){            return MyService.this;        }    }    public static interface Callback{        void onDataChange(String str);    }}

Activity

package com.example.halfopen.mytools;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class ServiceActivity extends Activity implements View.OnClickListener, ServiceConnection {    MyService.SetBinder binder;    TextView text;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_service);        text = (TextView) findViewById(R.id.text);        findViewById(R.id.btn_startService).setOnClickListener(this);        findViewById(R.id.btn_stopService).setOnClickListener(this);        findViewById(R.id.btn_bindService).setOnClickListener(this);        findViewById(R.id.btn_unbindService).setOnClickListener(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public void onClick(View v) {        Intent it =new Intent(ServiceActivity.this, MyService.class);        switch (v.getId()){            case R.id.btn_startService:                Toast.makeText(ServiceActivity.this, "启动服务", Toast.LENGTH_SHORT).show();                startService(it);                break;            case R.id.btn_stopService:                Toast.makeText(ServiceActivity.this, "停止服务", Toast.LENGTH_SHORT).show();                stopService(it);                break;            case R.id.btn_bindService:                Toast.makeText(ServiceActivity.this, "绑定服务", Toast.LENGTH_SHORT).show();                bindService(it, this, Context.BIND_AUTO_CREATE);                break;            case R.id.btn_unbindService:                Toast.makeText(ServiceActivity.this, "解除绑定", Toast.LENGTH_SHORT).show();                try{                    unbindService(this);                }catch (Exception e){                    e.printStackTrace();                }                break;        }    }    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        Toast.makeText(ServiceActivity.this, "服务绑定成功", Toast.LENGTH_SHORT).show();        binder = (MyService.SetBinder) service;        binder.getService().setCallback(new MyService.Callback() {            @Override            public void onDataChange(String str) {                //获取myService内部的数据                Message msg = new Message();                Bundle b = new Bundle();                b.putString("data", str);                msg.setData(b);                handler.sendMessage(msg);            }        });    }    @Override    public void onServiceDisconnected(ComponentName name) {        Toast.makeText(ServiceActivity.this, "服务结束", Toast.LENGTH_SHORT).show();    }    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            text.setText(msg.getData().getString("data"));        }    };}


0 0
原创粉丝点击