Service学习笔记-基础
来源:互联网 发布:typescript python 编辑:程序博客网 时间:2024/06/05 16:44
今天学了android service,记录下一些知识点。
1、Service的生命周期
2、Service两种调用方式
方式1:
activity中启动 Intent intent1=new Intent(this, MyService.class); intent1.putExtra("intent1",1); startService(intent1); activity中终止 stopService(new Intent(this, MyService.class)); Service中参数调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Trace.d(TAG,”onStartCommand”);
if(intent!=null && intent.getIntExtra(“intent1”,0)==1) Trace.d(TAG,”onStartCommand,intent1”);
else if(intent!=null && intent.getIntExtra(“intent2”,0)==1) Trace.d(TAG,”onStartCommand,intent2”);
return START_STICKY;
}
方式2:
activity中启动
bindService(new Intent(this, MyService.class), connection,BIND_AUTO_CREATE);
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Trace.d(TAG,”onServiceConnected”);
//isBind=true;
binder = (MyService.MyBinder) iBinder;
binder.startDownload();
}
@Override public void onServiceDisconnected(ComponentName componentName) { //isBind=false; Trace.d(TAG,"onServiceDisconnected"); }}; try { unbindService(connection); } catch (Exception e) { e.printStackTrace(); }
activity中终止
try {
unbindService(connection);
} catch (Exception e) {
e.printStackTrace();
}
Service中方法调用
class MyBinder extends Binder{
public void startDownload(){
Trace.d(TAG,”start startDownload^^^^^^^^”);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(1000);
Trace.d(TAG,”new Thread(new Runnable()),i=”+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stopSelf();
}
}).start();
Trace.d(TAG,”end startDownload^^^^^^^^”);
}
3、前端服务
Intent notificationIntent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
Notification notification=new Notification.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setLargeIcon(drawableToBitmap(getResources().getDrawable(R.mipmap.icon)))
.setContentTitle(“title”)
.setContentText(“Message”)
.setContentIntent(pendingIntent)
.getNotification();
startForeground(1,notification);
个人demo地址:http://download.csdn.net/detail/u013795543/9649857
- Service学习笔记-基础
- Android开发学习笔记-9 Service基础
- Android基础笔记----Service
- 【Android】Service基础学习
- Service 服务基础学习
- Web Service学习笔记(二)[web service的三大技术基础]
- Web service学习笔记
- Web Service学习笔记
- web service学习笔记
- Web service学习笔记
- Web Service学习笔记
- Android Service学习笔记
- Web Service学习笔记
- Web Service学习笔记
- web service 学习笔记
- Web Service学习笔记
- Android Service学习笔记
- Web Service学习笔记
- idea14 tomcat7 热部署
- java String.split丢失结尾空字符串的问题
- 戳中笑点
- 空字符串在String.split中的坑
- phantomjs+selenium爬取拉勾招聘信息
- Service学习笔记-基础
- Android 相机开发那些坑
- Android请求注册服务过程源码分析
- xiaoxiaole
- av_find_best_stream
- 最短路计数
- 8.3 自定义 Git - Git 钩子
- Java 多线程
- Oracle 中的 TO_DATE 和 TO_CHAR 函数 日期处理