android service基本操作

來源:互聯網
上載者:User

標籤:

service 四大組件之一,開啟後可以一直運行在系統後台。至於其他概念性問題我就不說了,直接操作把  具體內容在代碼注釋中有

先看下

上布局檔案把

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     tools:context=".MainActivity" > 7  8     <TextView 9         android:id="@+id/textView1"10         android:layout_width="wrap_content"11         android:layout_height="wrap_content"12         android:text="@string/hello_world" />13 14     <Button15         android:id="@+id/button1"16         android:layout_width="wrap_content"17         android:layout_height="wrap_content"18         android:text="啟動服務"19         android:onClick="startService" />20 21     <Button22         android:id="@+id/button2"23         android:layout_width="wrap_content"24         android:layout_height="wrap_content"25          android:text="停止服務"26         android:onClick="stopService" />27 28     <Button29         android:id="@+id/button3"30         android:layout_width="wrap_content"31         android:layout_height="wrap_content"32          android:text="綁定服務"33         android:onClick="bindService" />34 35     <Button36         android:id="@+id/button4"37         android:layout_width="wrap_content"38         android:layout_height="wrap_content"39         android:text="解除綁定服務"40         android:onClick="unbindService" />41 42 </LinearLayout>

重點看java代碼把

package com.zw.service;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {private Intent intent;private MyServiceCon conn = new MyServiceCon();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent = new Intent("com.zw.service.MyService");//Intent intent = new Intent(this,MyService.class);}public void startService(View v){startService(intent);}public void stopService(View v){stopService(intent);}//綁定服務public void bindService(View v){bindService(intent, conn, Context.BIND_AUTO_CREATE);}public void unbindService(View v){unbindService(conn);}//class MyServiceCon implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//綁定完成後回調方法(Activity) 和 Service 的串連}@Overridepublic void onServiceDisconnected(ComponentName name) {//服務解除綁定就調}}}

  service類

package com.zw.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("MyService-----onCreate");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);System.out.println("MyService-----onStart");}@Overridepublic IBinder onBind(Intent arg0) {System.out.println("MyService-----onBind");return null;}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("MyService-----onUnbind");return super.onUnbind(intent);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("MyService-----onDestroy");super.onDestroy();}}

  接下來要說的是在資訊清單檔中配置service

  <service android:name="com.zw.service.MyService">            <intent-filter >                <action android:name="com.zw.service.MyService"/>                            </intent-filter>        </service>

好了 自己運行看看把

 

android service基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.