日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Service详解(二)第一个Service

發(fā)布時(shí)間:2025/4/16 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Service详解(二)第一个Service 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????Service中有四個(gè)重要函數(shù): ?

????public?IBinder?onBind(Intent?arg0);????//必須實(shí)現(xiàn),返回接口給Servicepublic?void?onCreate();????????????????//Service創(chuàng)建時(shí)調(diào)用public?void?onStart(Intent?intent,int?startId);//通過startService()會(huì)調(diào)用public?void?onDestroy();????????????????//銷毀時(shí)StopService()調(diào)用

?

通過StartActivity()函數(shù)啟動(dòng)Service,當(dāng)?shù)谝淮握{(diào)用時(shí)會(huì)分別調(diào)用onCreate()和onStart在();

之后只會(huì)調(diào)用onStart();

通過函數(shù)StopService()結(jié)束Service,會(huì)調(diào)用onDestroy();

調(diào)用BindService():當(dāng)Service未創(chuàng)建時(shí)調(diào)用onCreate()和onBind();當(dāng)創(chuàng)建了只調(diào)用onBind();

使用函數(shù)bindService()和函數(shù)unbindService()可以綁定和解除綁定

對已經(jīng)綁定的Service調(diào)用bindService()無效,即多次調(diào)用bindService()和調(diào)用一次bindService()一樣。 unbindService()只能使用一次,即對于一個(gè)綁定的Service,只能調(diào)用一次unbindService(),多次調(diào)用會(huì)產(chǎn)生錯(cuò)誤


該函數(shù)原型為:

bindService(Intent,ServiceConnection,BIND_AUTO_CREATE);

ServiceConnection是一個(gè)服務(wù)連接類,必須實(shí)現(xiàn)以下兩個(gè)函數(shù):

public?void?onServiceConnected(ComponentName?arg0,?IBinder?arg1)//連接成功時(shí)調(diào)用 public?void?onServiceDisconnected(ComponentName?arg0)????????//連接失敗時(shí)調(diào)用

????示例如下:

private?ServiceConnection?conn=new?ServiceConnection()?{@Overridepublic?void?onServiceConnected(ComponentName?arg0,?IBinder?arg1)?{//?TODO?Auto-generated?method?stubToast.makeText(MainActivity.this,?"success",?Toast.LENGTH_LONG).show();Log.i("SERVICE","success");}@Overridepublic?void?onServiceDisconnected(ComponentName?arg0)?{//?TODO?Auto-generated?method?stubToast.makeText(MainActivity.this,?"errer",?Toast.LENGTH_LONG);Log.i("SERVICE","errer");}

????

????

Service實(shí)例:

????MainActivity.java:

private?ServiceConnection?conn=new?ServiceConnection()?{@Overridepublic?void?onServiceConnected(ComponentName?arg0,?IBinder?arg1)?{//?TODO?Auto-generated?method?stubToast.makeText(MainActivity.this,?"success",?Toast.LENGTH_LONG).show();Log.i("SERVICE","success");}@Overridepublic?void?onServiceDisconnected(ComponentName?arg0)?{//?TODO?Auto-generated?method?stubToast.makeText(MainActivity.this,?"errer",?Toast.LENGTH_LONG);Log.i("SERVICE","errer");} };protected?void?onCreate(Bundle?savedInstanceState)?{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button?button1=(Button)this.findViewById(R.id.btn1);Button?button2=(Button)this.findViewById(R.id.btn3);Button?button3=(Button)this.findViewById(R.id.btn4);Button?button4=(Button)this.findViewById(R.id.btn5);button1.setOnClickListener(new?OnClickListener()?{@Overridepublic?void?onClick(View?arg0)?{//?TODO?Auto-generated?method?stubstartService(new?Intent(MainActivity.this,NewService.class));}});button2.setOnClickListener(new?OnClickListener()?{@Overridepublic?void?onClick(View?arg0)?{//?TODO?Auto-generated?method?stubstopService(new?Intent(MainActivity.this,NewService.class));}});button3.setOnClickListener(new?OnClickListener()?{@Overridepublic?void?onClick(View?arg0)?{//?TODO?Auto-generated?method?stubbindService(new?Intent(MainActivity.this,NewService.class),conn,BIND_AUTO_CREATE);}});button4.setOnClickListener(new?OnClickListener()?{@Overridepublic?void?onClick(View?arg0)?{//?TODO?Auto-generated?method?stubunbindService(conn);}}); }

NewService.java:

public?class?NewService?extends?Service?{@Overridepublic?IBinder?onBind(Intent?arg0)?{//?TODO?Auto-generated?method?stubToast.makeText(NewService.this,?"onBind",?Toast.LENGTH_LONG).show();Log.i("SERVICE","onbind");return?null;}public?void?onCreate()?{super.onCreate();Log.i("SERVICE","oncreat");Toast.makeText(NewService.this,?"onCreat",?Toast.LENGTH_LONG).show();}public?void?onStart(Intent?intent,int?startId)?{Log.i("SERVICE","onstart");Toast.makeText(NewService.this,?"onStart",?Toast.LENGTH_LONG).show();}public?void?onDestroy()?{Log.i("SERVICE","ondestory");Toast.makeText(NewService.this,?"onDestory",?Toast.LENGTH_LONG).show();} }

Activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"?><Button?android:id="@+id/btn1"?android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="start"/><Button?android:id="@+id/btn3"?android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="stop"/><Button?android:id="@+id/btn4"?android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bind"/><Button?android:id="@+id/btn5"?android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="unbind"/>???? </LinearLayout>

AndroidManifest.xml增加:

?<service?android:name="com.example.new1.NewService"/>




轉(zhuǎn)載于:https://blog.51cto.com/aslonely/1616665

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Android Service详解(二)第一个Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。