Android 服务的入门
来源:互联网 发布:access数据库格式 编辑:程序博客网 时间:2024/05/17 08:39
public class MainActivity extends AppCompatActivity { private Myconn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击按钮 开启服务 通过startservice public void click1(View v) { Intent intent = new Intent(this, DemoService.class); startService(intent); // 开启服务 } public void click2(View v) { Intent intent = new Intent(this, DemoService.class); stopService(intent); } //点击按钮 绑定服务 开启服务的第二种方式 public void click3(View v) { Intent intent = new Intent(this,DemoService.class); //连接到DemoService这个服务 conn = new Myconn(); bindService(intent, conn, BIND_AUTO_CREATE); } //点击按钮手动解绑服务 public void click4(View v) { unbindService(conn); } @Override protected void onDestroy() { //当Activity销毁的时候 要解绑服务 unbindService(conn); super.onDestroy(); } //定义一个类 用来监视服务的状态 private class Myconn implements ServiceConnection{ //当服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("jojo", "onServiceConnected: "); } //失去连接时调用 @Override public void onServiceDisconnected(ComponentName name) { } }}
public class DemoService extends Service { private static final String TAG = "jojo"; public DemoService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service.// throw new UnsupportedOperationException("Not yet implemented"); Log.d(TAG, "onBind: "); return null; } //当服务第一次创建的时候调用 @Override public void onCreate() { Log.d(TAG, "onCreate: "); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand: "); return super.onStartCommand(intent, flags, startId); } //当服务销毁的时候调用 @Override public void onDestroy() { Log.d(TAG, "onDestroy: "); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "onUnbind: "); return super.onUnbind(intent); }}
0 0
- Android 服务的入门
- Android开发入门之服务的生命周期
- android入门之服务
- Android使用WCF的服务程序之入门
- Android入门:绑定本地服务
- Android入门:绑定本地服务
- Android入门(52)——第七章 Android中常用的系统服务
- android SystemService简单入门 初识系统服务
- Android开发入门之建立能与访问者进行相互通信的本地服务
- [Android开发入门]调用系统服务之Manager的使用(一)
- Android 服务的生命周期
- android的系统服务
- Android的位置服务
- Android服务的生命周期
- Android的系统服务
- Android服务的生命周期
- Android的系统服务
- Android的AlarmManager服务
- win7 64 安装mysql-python:fatal error C1083: Cannot open include file: 'config-win.h': No such file or
- 第二只爬虫---百度贴吧
- 建造者模式
- 类选择器(谷歌浏览器图标)
- springMVC+spring+maven+mybatis第一篇配置
- Android 服务的入门
- 创建用户并授权
- ZigBee终端怎样检测协调器或路由器已不在网络中
- Git 配置SSH简单玩法?
- JAVA输出九九乘法表,用for,dowhile,while循环语句输出
- 通信中的极化隔离
- 微信小程序学习网站
- SCU4439-Vertex Cover
- 多类选择器