Android中Activity与Service之间的通信

来源:互联网 发布:mac 搜索快捷键 编辑:程序博客网 时间:2024/05/16 07:48

首先,说一下Activity之间的信息传递方式:

这个众所周知,Activity之间通信是通过Bundle对象中添加参数,通过intent传递给对方,然后对方通过getIntent()方法接收intent对象。

FirstActivity的发送参数代码:

 
Intent intent=new Intent(FirstActivity,SecondActivity);Bundle bundle=new Bundle();bundle.putString("name",username);bundle.putString("password",password);intent.putExtras(bundle);startActivity(intent);

SecondActivity的接收参数代码:

Intent intent=getIntent();String username=getStringExtra("name");String password=getStringExtra("password");

另一种情况是,Activity传递参数给Service(也就是Activity和Service绑定,通过bindService()方法启动Service):

1.首先定义一个MyService类,继承Service。

package eoe.demo;import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private MyBinder myBinder = new MyBinder(); // 成功绑定后调用该方法 @Override public IBinder onBind(Intent intent) { Log.d("MyService", "onBind"); return myBinder; } // 重新绑定时调用该方法 @Override public void onRebind(Intent intent) { Log.d("MyService", "onRebind"); super.onRebind(intent); } // 解除绑定时调用该方法 @Override public boolean onUnbind(Intent intent) { Log.d("MyService", "onUnbind"); return super.onUnbind(intent); } @Override public void onCreate() { Log.d("MyService", "onCreate"); super.onCreate(); } @Override public void onDestroy() { Log.d("MyService", "onDestroy"); super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { Log.d("MyService", "onStart"); super.onStart(intent, startId); } 
//通过定义一个MyBinder内部类来得到Service对象public class MyBinder extends Binder { 
public MyService getService() { return MyService.this; } } } 

2.第二部,就要在定义一个MainActivity,在Activity中实现一个ServiceConection接口,通过接口来获取Service对象,然后就可以在Activity中操作Service对象了。

class MainActivity extends Activity{    public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);        private ServiceConnection serviceConnection = new ServiceConnection() {     // 连接服务失败后,该方法被调用     @Override     public void onServiceDisconnected(ComponentName name) 
    {     myService = null;     Toast.makeText(Main.this, "Service Failed.", Toast.LENGTH_LONG).show();     }     // 成功连接服务后,该方法被调用。在该方法中可以获得MyService对象     @Override     public void onServiceConnected(ComponentName name, IBinder service)
    {     // 获得MyService对象      myService = ((MyService.MyBinder) service).getService();     Toast.makeText(Main.this, "Service Connected.", Toast.LENGTH_LONG).show();     } }; //然后就要通过bindservice()方法,绑定并开启服务 
    Intent intent=new Intent();    Bundle bundle=new Bundle();    bundle.putString("name",name);    bundle.putString("password",password);    intent.putExtras(bundle);
    //绑定
    bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    //解除绑定
    unbindService(serviceConnection);
    }   }

总结: 在MyService类中定义了一个MyBinder类,该类实际上是为了获得MyService的对象实例的。在ServiceConnection接口的onServiceConnected方法中的第2个参数是一个IBinder类型的变量,将该参数转换成MyService.MyBinder对象,并使用MyBinder类中的getService方法获得MyService对象。在获得MyService对象后,就可以在Activity中随意操作MyService了。

	
				
		
原创粉丝点击