android 服务
2019獨角獸企業重金招聘Python工程師標準>>>
1.新建class 繼承android.app.Service;重寫其oncreat、onbind、ondestroy、onunbind方法
?package?com.example.services;
import?java.util.Timer;
import?java.util.TimerTask;
import?android.R.integer;
import?android.app.Service;
import?android.content.Intent;
import?android.os.Binder;
import?android.os.IBinder;
import?android.widget.Toast;
public? class?EchoServices?extends?Service?{
????@Override
???? public?IBinder?onBind(Intent?arg0)?{
????????Toast.makeText( this,? " 啟動服務onBind ",? 1).show();
????????startTimer();
???????? return?bindServices;
????}
????@Override
???? public?boolean?onUnbind(Intent?intent)?{
????????Toast.makeText( this,? " 關閉服務onUnbind ",? 1).show();
????????stopTimer();
???????? return?super.onUnbind(intent);
????}
????@Override
???? public? void?onCreate()?{
????????super.onCreate();
????????Toast.makeText( this,? " 啟動服務onCreat ",? 1).show();
????}
????@Override
???? public? void?onDestroy()?{
????????super.onDestroy();
????????Toast.makeText( this,? " 關閉服務onDestroy ",? 1).show();
????}
????
???? public?bindServices?bindServices?=? new?bindServices();
???? public? class?bindServices?extends?Binder?{
????????
???????? public?EchoServices?getEchoServices(){
???????????? return?EchoServices. this;
????????}
????}
????
???? private?Timer?timer= null;
???? private?TimerTask?task= null;
????
????
???? private? int?i= 0;
???? private? void?startTimer(){
???????? if(timer== null){
????????????timer= new?Timer();
????????????task= new?TimerTask()?{
????????????????@Override
???????????????? public? void?run()?{
????????????????????i++;
????????????????}
????????????};
????????????timer.schedule(task,? 1000,? 1000);
????????}
????}
???? private? void?stopTimer(){
???????? if(timer!= null){
????????????task.cancel();
????????????timer.cancel();
????????????timer= null;
????????????task= null;
????????}
????}
????
???? public? int?getCurrentNumber(){
???????? return?i;
????}
}
2.清單文件中加入服務組件 <? service? android:name? = "EchoServices" ></? service? >
3.聲明意圖對象表示服務,開啟服務關閉服務
??1?package?com.example.services;
?2??3? import?android.os.Bundle;
?4? import?android.os.IBinder;
?5? import?android.app.Activity;
?6? import?android.content.ComponentName;
?7? import?android.content.Context;
?8? import?android.content.Intent;
?9? import?android.content.ServiceConnection;
10? import?android.view.Menu;
11? import?android.view.View;
12? import?android.view.View.OnClickListener;
13? import?android.widget.Button;
14? import?android.widget.Toast;
15? import?android.widget.SearchView.OnCloseListener;
16?
17? public? class?MainActivity? extends?Activity? implements?OnClickListener,?ServiceConnection?{
18?
19????? private?Button?btn_start,?btn_stop,?btn_bind,?btn_unbind,btn_getNum;
20????? private?Intent?servicesIntent;
21????? private?EchoServices?echoServices;
22?
23?????@Override
24????? protected? void?onCreate(Bundle?savedInstanceState)?{
25????????? super.onCreate(savedInstanceState);
26?????????setContentView(R.layout.activity_main);
27?????????btn_start?=?(Button)?findViewById(R.id.btn_start);
28?????????btn_stop?=?(Button)?findViewById(R.id.btn_stop);
29?????????btn_bind?=?(Button)?findViewById(R.id.btn_bind);
30?????????btn_unbind?=?(Button)?findViewById(R.id.btn_unbind);
31?????????btn_getNum?=?(Button)?findViewById(R.id.btn_getNum);
32?????????btn_start.setOnClickListener( this);
33?????????btn_stop.setOnClickListener( this);
34?????????btn_bind.setOnClickListener( this);
35?????????btn_unbind.setOnClickListener( this);
36?????????btn_getNum.setOnClickListener( this);
37?????????servicesIntent?=? new?Intent( this,?EchoServices. class);
38?????}
39?
40?????@Override
41????? public? void?onClick(View?v)?{
42????????? int?id?=?v.getId();
43????????? switch?(id)?{
44????????? case?R.id.btn_start:
45?????????????startService(servicesIntent);
46????????????? break;
47????????? case?R.id.btn_stop:
48?????????????stopService(servicesIntent);
49????????????? break;
50????????? case?R.id.btn_bind:
51?????????????bindService(servicesIntent,? this,?Context.BIND_AUTO_CREATE);
52????????????? break;
53????????? case?R.id.btn_unbind:
54?????????????unbindService( this);
55????????????? break;
56????????? case?R.id.btn_getNum:
57????????????? if(echoServices!= null){
58?????????????????System.out.println(echoServices.getCurrentNumber());
59?????????????}
60?????????????
61????????????? break;
62?????????}
63?????
64?????}
65?
66????? /**
67? ?????*?當服務綁定成功時觸發
68? ????? */
69?????@Override
70????? public? void?onServiceConnected(ComponentName?arg0,?IBinder?binder)?{
71?????????Toast.makeText(getApplicationContext(),?"綁定成功",?1).show();
72?????????echoServices=((EchoServices.bindServices)binder).getEchoServices();
73?????}
74?
75????? /**
76? ?????*?當綁定失敗時觸發
77? ????? */
78?????@Override
79????? public? void?onServiceDisconnected(ComponentName?arg0)?{
80?????????Toast.makeText(getApplicationContext(),?"綁定失敗",?1).show();
81?????}
82?
83?}
?4.oncreat、onbind、ondestroy、onunbind方法的區別
采用Context.bindService()方法啟動服務,在服務未被創建時,系統會先調用服務的onCreate()方法,接著調用onBind()方法,這個時候訪問者和服務綁定在一起。?如果訪問者要與服務進行通信,那么,onBind()方法必須返回Ibinder對象。如果訪問者退出了,系統就會先調用服務的onUnbind()方法,接著調用onDestroy()方法。如果調用bindService()方法前服務已經被綁定,多次調用bindService()方法并不會導致多次創建服務及綁定(也就是說onCreate()和onBind()方法并不會被多次調用)。如果訪問者希望與正在綁定的服務解除綁定,可以調用unbindService()方法,調用該方法也會導致系統調用服務的onUnbind()-->onDestroy()方法。
Activity與服務進行通信,開發人員通常把通信方法定義在接口里,然后讓Ibinder對象實現該接口,而Activity通過該接口引用服務onBind()方法返回的Ibinder對象,然后調用Ibinder對象里自定義的通信方法。
?
轉載于:https://my.oschina.net/zhangjie9142/blog/495194
總結
以上是生活随笔為你收集整理的android 服务的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: [zz]GMM-HMM语音识别模型 原理
- 下一篇: CSDN下载频道2014年11月4日本-
