Android Service和Activity通信
来源:互联网 发布:数据库 id冗余名称 编辑:程序博客网 时间:2024/05/16 11:06
一,通过startService(Intent service)启动服务
只能通过广播实现通信
二,通过bindService(Intent service,ServiceConnection conn,int flags)启动服务
第一个参数如果只是调用setAction(String action)方法实现隐式的Intent调用Service会出现Implicit intents with startService are not safe错误,这是引文从Lollipop开始,service服务必须采用显示方式启动。
Android源码
改进方法:
1) 直接在Intent中指定要启动的Service
Intent intent = new Intent(xxx.this,xxx.class);
bindService(intent,ServiceConnection,flags);
2) 设置Action和PackageName
Intent intent = new Intent();
intent.setAction("xxx.xxx.xxx");
intent.setPackage(getPackageName());
bindService(intent,ServiceConnection,flags);
在调用bindService的Activity中实现定义ServiceConnection,并实现其中的方法
public void onServiceConnected(ComponentName name,IBinder service);//强转成自己实现的Binder对象,即可实现通信
public void onServiceDisconnected(ComponentName name);
在Service类中定义内部类实现Binder,在onBind回调方法中将Binder的实例返回。
实例:
public class MyService extends Service { private int count = 110; private String TAG = "Service"; private MyBinder binder = new MyBinder(); public class MyBinder extends Binder { public int getCount(){ return count; } } public MyService() { } @Override public void onCreate() { super.onCreate(); Log.e(TAG,"onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG,"onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG,"onDestroy"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.e(TAG,"onUnbind"); return super.onUnbind(intent); } @Override public IBinder onBind(Intent intent) { Log.e(TAG,"onBind"); return binder; }}
public class MainActivity extends Activity{ private Button bind,unbind; private MyService.MyBinder binder; ServiceConnection connectionn = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG,"onServiceConnected"); binder = (MyService.MyBinder)service; Log.e(TAG,binder.getCount() + ""); } @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG,"onServiceDisconnected is called"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bind= (Button)findViewById(R.id.bind); bind.setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v) {Intent intent = new Intent(MainActivity.this,MyService.class);}bindService(intent, connectionn, Service.BIND_AUTO_CREATE);});unbind= (Button)findViewById(R.id.unbind); unbind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {unbindService(connectionn);} }); }}
- Android Service和Activity通信
- Android Activity和Service之间的通信
- Android Activity和Service之间的通信
- Android Activity和Service之间的通信
- android activity和service通过contentprovider通信
- Android Activity和Service之间的通信
- Android笔记 Activity和Service的通信
- android Service和Activity的通信
- Activity和Service通信
- Activity和Service通信
- Activity和Service通信
- service和activity通信
- activity和service通信
- service和activity通信
- Activity和Service通信
- [android]Android Activity和Service之间的通信
- android activity与service通信
- Android Activity与Service通信
- wxPython笔记(Getting started with wxPython 3)
- 内存分析
- 微信扫码支付-原生支付-流程图
- opencv-压缩图片
- 数据挖掘算法基础-关联规则
- Android Service和Activity通信
- weblogic创建域(Linux系统)
- 基于HTML5树组件延迟加载技术实现
- GitHub 优秀的 Android 开源项目
- 聚集索引与非聚集索引
- Oracle11g ORA-12541
- 重写ToString()以实现定制化输出字符串
- 扫描识别系统能干些什么?
- 前后台传中文乱码问题改成UTF-8