Service1
2016-01-11 11:48
316 查看
原来 service 是指界面被关掉 还能继续运行的意思,运行在后台。
startservice
stopservice
绑定服务(关闭界面,会退出当前的app)
bindservice
unbindservice
基本用法如下:
package zhbit.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Using_ServiceActivity extends Activity implements OnClickListener, ServiceConnection {
/* Called when the activity is first created. /
private Button btnstartservice, btnstopservice, btnbindservice,
btnunbindservice;
private Intent intent1;
}
package zhbit.test;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class Second extends Service{
}
startservice
stopservice
绑定服务(关闭界面,会退出当前的app)
bindservice
unbindservice
基本用法如下:
package zhbit.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Using_ServiceActivity extends Activity implements OnClickListener, ServiceConnection {
/* Called when the activity is first created. /
private Button btnstartservice, btnstopservice, btnbindservice,
btnunbindservice;
private Intent intent1;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); intent1 = new Intent(Using_ServiceActivity.this, Second.class); btnstartservice = (Button) findViewById(R.id.StartService); btnstopservice = (Button) findViewById(R.id.stopService); btnbindservice = (Button) findViewById(R.id.BindService); btnunbindservice = (Button) findViewById(R.id.UnBindService); btnstartservice.setOnClickListener(this); btnstopservice.setOnClickListener(this); btnbindservice.setOnClickListener(this); btnunbindservice.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.StartService: startService(intent1); break; case R.id.stopService: stopService(intent1); break; case R.id.BindService: bindService(intent1, this, Context.BIND_AUTO_CREATE); break; case R.id.UnBindService: unbindService(this); // stopService(intent1); break; default: break; } } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub Toast.makeText(this, "onserviceconnected", Toast.LENGTH_LONG).show(); } public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Toast.makeText(this, "onservicedisconnected", Toast.LENGTH_LONG).show(); }
}
package zhbit.test;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class Second extends Service{
@Override public void onCreate() { Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show(); System.out.print("onCrate"); super.onCreate(); } @Override public void onDestroy() { Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show(); System.out.print("onDestroy"); super.onDestroy(); } private final EchServiceBinder echservicebinder = new EchServiceBinder(); public class EchServiceBinder extends Binder{ } @Override public IBinder onBind(Intent intent) { Toast.makeText(this, "onBind", Toast.LENGTH_LONG).show(); return echservicebinder; }
}
相关文章推荐
- 个人信息安全报告发布:有 APP 每分钟调用位置权限 1468 次
- 下载量超过一亿的流行应用被发现含有恶意模块
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- APP添加CNZZ统计插件教程 Android版添加phonegap
- Android APP与媒体存储服务的交互
- java实现收藏名言语句台词的app
- 修改Android App样式风格的方法
- Android App数据格式Json解析方法和常见问题
- 通过Html网页调用本地安卓(android)app程序代码
- 百度地图API提示230 错误app scode码校验失败的解决办法
- js判断移动端是否安装某款app的多种方法
- PHP实现批量生成App各种尺寸Logo
- Could not load file or assembly "App_Licenses.dll"的问题
- Android桌面组件App Widget用法入门教程
- 开源电商app常用标签"hot"之第三方开源LabelView
- 基于App自适应draw9patch不失真背景的方法详解
- 安卓APP测试之使用Burp Suite实现HTTPS抓包方法
- web app与原生app的区别
- 使用Chrome浏览器调试Android App详解