日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android Service详解(二)第一个Service

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

????Service中有四個重要函數: ?

????public?IBinder?onBind(Intent?arg0);????//必須實現,返回接口給Servicepublic?void?onCreate();????????????????//Service創建時調用public?void?onStart(Intent?intent,int?startId);//通過startService()會調用public?void?onDestroy();????????????????//銷毀時StopService()調用

?

通過StartActivity()函數啟動Service,當第一次調用時會分別調用onCreate()和onStart在();

之后只會調用onStart();

通過函數StopService()結束Service,會調用onDestroy();

調用BindService():當Service未創建時調用onCreate()和onBind();當創建了只調用onBind();

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

對已經綁定的Service調用bindService()無效,即多次調用bindService()和調用一次bindService()一樣。 unbindService()只能使用一次,即對于一個綁定的Service,只能調用一次unbindService(),多次調用會產生錯誤


該函數原型為:

bindService(Intent,ServiceConnection,BIND_AUTO_CREATE);

ServiceConnection是一個服務連接類,必須實現以下兩個函數:

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

????示例如下:

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實例:

????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"/>




轉載于:https://blog.51cto.com/aslonely/1616665

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。