BindService与StartService的综合使用案例

       这一节,来复习一下BindService与StartService的综合使用,实现的效果就是程序一开始运行,就不断在媒体中心查找歌曲,看一看是否有歌曲更新或删除。

MainActivity代码:

public class MainActivity extends ListActivity {
    //LoaderManager用于异步加载数据,当然,它异步的实现依赖于AsyncTask的原理
    LoaderManager loader;
    //SimpleCursorAdapter用于将Cursor中的columnsXML文件中定义的TextView或者ImageView进行匹配的Adapter
    private SimpleCursorAdapter adapter;
    private IMusicLoader musicLoader;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        loader = getLoaderManager();
        handler=new ServiceHandler();
        Intent intent = new Intent(MainActivity.this, MusicServices.class);
        bindService(intent,conn, Context.BIND_AUTO_CREATE);
        adapter = new SimpleCursorAdapter(MainActivity.this,
         android.R.layout.simple_list_item_1,
         null,new String[]{MediaStore.Audio.Media.DISPLAY_NAME}
        ,new int[]{android.R.id.text1}
        , CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        getListView().setAdapter(adapter);
    }
    private ServiceConnection conn=new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
         musicLoader= (IMusicLoader) iBinder;
         musicLoader.onInit(loader,handler);
         startService(new Intent(MainActivity.this,MusicServices.class));
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };
    class ServiceHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            System.out.println("MainActivity--->ServiceHandler ");
            adapter.swapCursor((Cursor) msg.obj);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
 
  
IMusicLoader代码:
//用于初始化LoaderMangerHandler的接口
public interface IMusicLoader {
    public void onInit(LoaderManager loaderManager, Handler handlder);
}
MusicServices代码:
public class MusicServices extends Service implements LoaderManager.LoaderCallbacks{
   private LoaderManager loaderManager;
   private Handler handler;
    public MusicServices() {
    }
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("MusicServices------>onCreate");
    }
    private boolean isLoader=true;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("MusicServices----->onStartCommand");
        if(loaderManager!=null&&isLoader){
            loaderManager.initLoader(0,null,this);
            isLoader=false;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("MusicServices---->onBind");
        return new MyBinder();
    }
    @Override
    public Loader onCreateLoader(int id, Bundle bundle) {
        if(id!=0){
            return null;
        }
        //重新查找
        return new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                ,null,null,null,null);
    }
    @Override
    public void onLoadFinished(Loader loader, Cursor cursor) {
        //查询返回的cursor对象如何更新activity
        System.out.println("MusicServices--->onLoadFinished ");
        new Message().obtain(handler,1,cursor).sendToTarget();
    }
    @Override
    public void onLoaderReset(Loader loader) {
    }
    //信使
    class MyBinder extends Binder implements IMusicLoader{
        @Override
        public void onInit(LoaderManager loaderManagers, Handler handlders) {
            MusicServices.this.loaderManager=loaderManagers;
            MusicServices.this.handler=handlders;
        }
    }
}
运行效果:
图一:
图二:
程序运行后,效果为图一,最小化后,然后在音乐App中删除张敬轩的<<衬>>,再打开程序,效果为图二。




 
  

你可能感兴趣的:(android,Android基础)