欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Service的基本操作-創(chuàng)新互聯(lián)

   Service的基本操作,啟動(dòng)service的方式有兩種,一種是context.startService,暫停的service是stopService,這種方式service與主activity沒有關(guān)聯(lián),不能單純的進(jìn)行數(shù)據(jù)交互(可以考慮使用廣播,進(jìn)行交互),另一種方式binderService,這種方式返回的是一個(gè)binder對(duì)象,

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),貢嘎網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:貢嘎等地區(qū)。貢嘎做網(wǎng)站價(jià)格咨詢:028-86922220

   binderService(Intent service,ServiceConnection conn,int flag):第一個(gè)參數(shù)是intent對(duì)象,第二個(gè)是鏈接對(duì)象,第三個(gè)是否自動(dòng)創(chuàng)建service,0是不自動(dòng)創(chuàng)建,BINDER_AUTO_CREATE

public class MyService extends Service {

public MyService() {

}

public int count = 0 ;

public MyBinder binder = new MyBinder();

public class MyBinder extends Binder {

// 創(chuàng)建一個(gè)集成Binder類的對(duì)象,activity調(diào)用service的時(shí)候返回一個(gè)Binder對(duì)象給activity

public int getCount(){

count = 10 ;

return count ;

}

}

@Override

public IBinder onBind(Intent arg0) {

// 必須實(shí)現(xiàn)的方法

return binder;

}

@Override

public void onCreate() {

// 每次創(chuàng)建的時(shí)候執(zhí)行一次,

System.out.println("onCreate");

super.onCreate();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// service每次運(yùn)行的時(shí)候執(zhí)行

System.out.println("onStartCommand");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

// 銷毀的之后執(zhí)行

System.out.println("onDestroy");

super.onDestroy();

}

}

public class MainActivity extends Activity implements OnClickListener {

private Button start, stop, get;

MyService.MyBinder binder;

private ServiceConnection conn = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

// 鏈接斷開的時(shí)候進(jìn)行掉用

System.out.println("service鏈接斷開");

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// 進(jìn)行鏈接的時(shí)候進(jìn)行調(diào)用

System.out.println("service鏈接成功");

binder = (MyService.MyBinder) service;

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

start = (Button) findViewById(R.id.start);

stop = (Button) findViewById(R.id.stop);

get = (Button) findViewById(R.id.get);

this.start.setOnClickListener(this);

this.stop.setOnClickListener(this);

this.get.setOnClickListener(this);

}

@Override

public void onClick(View view) {

switch (view.getId()) {

case R.id.start:

// 第二種啟動(dòng)方式

Intent in = new Intent(MainActivity.this, MyService.class);

bindService(in, conn, Service.BIND_AUTO_CREATE);

// 第一種啟動(dòng)方式,activity關(guān)閉的時(shí)候service也會(huì)隨著關(guān)閉

// Intent in = new Intent(MainActivity.this,MyService.class) ;

// MainActivity.this.startService(in) ;

break;

case R.id.stop:

// 第二種關(guān)閉方式

unbindService(conn);

// 第一種啟動(dòng)方式,activity關(guān)閉的時(shí)候service也會(huì)隨著關(guān)閉

// Intent in2 = new Intent(MainActivity.this,MyService.class) ;

// MainActivity.this.stopService(in2) ;

break;

case R.id.get:

// 獲取狀態(tài)

System.out

.println("=====service返回的值======>>>>" + binder.getCount());

break;

default:

break;

}

}

}

附件:http://down.51cto.com/data/2365688

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。

網(wǎng)頁名稱:Service的基本操作-創(chuàng)新互聯(lián)
文章URL:http://www.chinadenli.net/article24/jgoje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)定制開發(fā)營(yíng)銷型網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)