大家好,又见面了,我是你们的朋友全栈君。
1. Started Service中使用startService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行 【onCreate()- >onStartCommand()->startService()->onDestroy()】,注意其中没有 onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。
2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】。
example:service与activity间通信
MainActivity
1 package com.stone.test2; 2 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.IBinder; 8 import android.os.Message; 9 import android.support.v7.app.ActionBarActivity; 10 import android.os.Bundle; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.View; 14 import android.widget.EditText; 15 import android.widget.TextView; 16 17 import java.util.logging.Handler; 18 import java.util.logging.LogRecord; 19 20 21 public class MainActivity extends ActionBarActivity implements View.OnClickListener, ServiceConnection { 22 23 private EditText edittext; 24 private TextView textview; 25 private MyService.MyBinder myBinder; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 edittext= (EditText) findViewById(R.id.edittext); 32 textview= (TextView) findViewById(R.id.textview); 33 System.out.println("onCreate"); 34 findViewById(R.id.btnStart).setOnClickListener(this); 35 findViewById(R.id.btnStop).setOnClickListener(this); 36 findViewById(R.id.btnBindService).setOnClickListener(this); 37 findViewById(R.id.btnUnbindService).setOnClickListener(this); 38 findViewById(R.id.btnSyn).setOnClickListener(this); 39 40 } 41 42 @Override 43 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 44 super.onActivityResult(requestCode, resultCode, data); 45 textview.setText(data.getStringExtra("data")); 46 } 47 48 @Override 49 public boolean onCreateOptionsMenu(Menu menu) { 50 // Inflate the menu; this adds items to the action bar if it is present. 51 getMenuInflater().inflate(R.menu.menu_main, menu); 52 return true; 53 54 } 55 56 @Override 57 public boolean onOptionsItemSelected(MenuItem item) { 58 // Handle action bar item clicks here. The action bar will 59 // automatically handle clicks on the Home/Up button, so long 60 // as you specify a parent activity in AndroidManifest.xml. 61 int id = item.getItemId(); 62 63 //noinspection SimplifiableIfStatement 64 if (id == R.id.action_settings) { 65 return true; 66 } 67 68 return super.onOptionsItemSelected(item); 69 } 70 71 @Override 72 public void onClick(View v) { 73 switch (v.getId()) { 74 case R.id.btnStart: 75 // System.out.println("who are you"); 76 Intent intent = new Intent(this,MyService.class); //starService通过intent把数据传给MyService 77 intent.putExtra("data",edittext.getText().toString()); 78 startService(intent); 79 break; 80 case R.id.btnStop: //stopService 81 stopService(new Intent(this, MyService.class)); 82 break; 83 case R.id.btnBindService: //binService,通过实现ServiceConnection,将数据传给MyService 84 bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE); 85 break; 86 case R.id.btnUnbindService: 87 unbindService(this); 88 break; 89 case R.id.btnSyn: //同步数据,将数据传给MyService 90 if (myBinder!=null){ 91 myBinder.setData(edittext.getText().toString()); 92 } 93 break; 94 } 95 } 96 97 @Override 98 public void onServiceConnected(ComponentName name, IBinder service) { 99 myBinder = (MyService.MyBinder) service; //同步数据,将数据传给MyService 100 myBinder.getService().setCallback(new MyService.callBack() { //获取MyService传来的数据 101 @Override 102 public void onDataChange(String data) { //因为在MyService中是在另一个线程中调用onDataChange,所以通过handler将数据传送给UI线程 103 Message msg = new Message(); 104 Bundle bundle = new Bundle(); 105 bundle.putString("data",data); 106 msg.setData(bundle); 107 handler.sendMessage(msg); 108 } 109 }); 110 } 111 112 @Override 113 public void onServiceDisconnected(ComponentName name) { 114 115 } 116 117 private android.os.Handler handler = new android.os.Handler(){ //通过Handler获取工作线程传来的数据,并显示在TextView中 118 @Override 119 public void handleMessage(Message msg) { 120 super.handleMessage(msg); 121 textview.setText(msg.getData().getString("data")); 122 } 123 }; 124 }
MyService
1 package com.stone.test2; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyService extends Service { 9 private String data; 10 private boolean running; 11 12 public MyService() { 13 } 14 15 @Override 16 public IBinder onBind(Intent intent) { 17 return new MyBinder(); 18 } 19 20 public class MyBinder extends Binder{ 21 22 public void setData(String data){ //bindservice方式获取Activity传送来的数据 23 MyService.this.data=data; 24 } 25 26 public MyService getService(){ //返回MyService 27 return MyService.this; 28 } 29 } 30 31 @Override 32 public int onStartCommand(Intent intent, int flags, int startId) { 33 data = intent.getStringExtra("data"); //startService方式获取Activity传送来的数据 34 return super.onStartCommand(intent, flags, startId); 35 } 36 37 @Override 38 public void onCreate() { 39 super.onCreate(); 40 running = true; 41 new Thread(){ 42 @Override 43 public void run() { 44 super.run(); 45 int i = 0; 46 // System.out.println("fucking"); 47 while (running) { 48 i++; 49 String str=i+":" + data; 50 if (callback!=null) { 51 callback.onDataChange(str); 52 } 53 System.out.println(str); 54 try { 55 sleep(1000); 56 } catch (InterruptedException e) { 57 e.printStackTrace(); 58 } 59 } 60 } 61 }.start(); 62 63 } 64 65 @Override 66 public void onDestroy() { 67 super.onDestroy(); 68 running = false; 69 } 70 71 private callBack callback = null; 72 73 public void setCallback(callBack callback){ 74 this.callback = callback; 75 } 76 77 public callBack getCallback(){ 78 return callback; 79 } 80 81 public static interface callBack{ //bindService通过回调函数将数据传送给Activity 82 void onDataChange(String data); 83 } 84 }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155580.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...