Android笔记三十四.Service综合实例二
- package com.example.aildserver;
- interface Song
- {
- String getName();
- String getSong();
- }
- package com.example.aildserver;
- import com.example.aildserver.Song.Stub;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.RemoteException;
- public class MyService extends Service {
- private String[] names = new String[] {"林俊杰","蔡依林","邓紫棋"};
- private String[] songs = new String[] {"可惜没如果","第三人称","多远都要在一起"};
- private String name,song;
- private int current=1; //当前位置
- private MyBinder binder = new MyBinder(); //实例化一个IBinder对象
- /*0.Stub内部类
- * 该内部类实现了IBinder、Song两个接口,这个Stub类将会作为远程Service的回调类。*/
- public class MyBinder extends Stub
- {
- //a.客户端回调该方法获取歌手名
- public String getName() throws RemoteException
- {
- return name;
- }
- //b.客户端回调该方法获取歌曲
- public String getSong() throws RemoteException
- {
- return song;
- }
- }
- /*1.onBind方法
- * service用于返回一个IBinder对象给客户端方便通信
- */
- @Override
- public IBinder onBind(Intent arg0) {
- return binder;
- }
- /*2.onCreate方法
- * 当Service启动后,自动调用该方法,用于初始化
- * */
- public void onCreate() {
- name = names[current]; //给name、song赋值
- song = songs[current];
- System.out.println("Service print:name="+name+"song="+song);
- super.onCreate();
- }
- /*3.onDestroy方法
- * 当访问者调用Context.stopService方法后,调用该方法关闭Service服务
- * */
- public void onDestroy() {
- super.onDestroy();
- }
- /*4.onUnbind方法
- * 当访问者调调用Context.unBind()方法后,调用该方法与Service解除绑定*/
- public boolean onUnbind(Intent intent) {
- return false;
- }
- }
- <application
- ........
- <!-- 配置service -->
- <service android:name=".MyService">
- <intent-filter>
- <action android:name="com.jiangdongguo.service"/>
- </intent-filter>
- </service>
- </application>
- package com.example.aildclient;
- import com.example.aildserver.Song;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- private Button getBtn;
- private EditText song;
- private EditText name;
- private Song binder;
- //1.创建一个ServiceConnection对象
- private ServiceConnection conn = new ServiceConnection()
- {
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- binder = Song.Stub.asInterface(service); //获取Service返回的代理IBinder对象
- }
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getBtn=(Button)findViewById(R.id.get);
- song=(EditText)findViewById(R.id.song);
- name=(EditText)findViewById(R.id.name);
- //2.指定要启动的Service
- Intent intent = new Intent("com.jiangdongguo.service");
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- getBtn.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0)
- {
- try {
- name.setText(binder.getName());
- song.setText(binder.getSong());
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。