android应用开发详解(十七)---------------service初级

来源:互联网 发布:有答案中级java面试题 编辑:程序博客网 时间:2024/05/16 16:21

1、工程目录


2、MainActivity.java

package com.example.test_service;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;public class MainActivity extends Activity {private Button startBtn, stopBtn, bindBtn, unbindBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);startBtn = (Button) findViewById(R.id.button01);stopBtn = (Button) findViewById(R.id.button02);bindBtn = (Button) findViewById(R.id.button03);unbindBtn = (Button) findViewById(R.id.button04);startBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");startService(intent);}});stopBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");stopService(intent);}});final ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName arg0) {// TODO Auto-generated method stubLog.i("SERVICE", "断开连接");Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG).show();}@Overridepublic void onServiceConnected(ComponentName arg0, IBinder arg1) {// TODO Auto-generated method stubLog.i("SERVICE", "连接成功");Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG).show();}};bindBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");bindService(intent, conn, Service.BIND_AUTO_CREATE);}});unbindBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");unbindService(conn);}});}}

3、MyService.java

package com.example.test_service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MyService extends Service{@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i("SERVICE", "on create.................");Toast.makeText(MyService.this,"on create .........." , Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i("SERVICE", "on destroy.................");Toast.makeText(MyService.this,"on destroy .........." , Toast.LENGTH_LONG).show();}@Override@Deprecatedpublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);Log.i("SERVICE", "on start.................");Toast.makeText(MyService.this,"on start .........." , Toast.LENGTH_LONG).show();}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubLog.i("SERVICE", "on bind.................");Toast.makeText(MyService.this,"on bind .........." , Toast.LENGTH_LONG).show();return null;}}


4、main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="启动Service" />    <Button        android:id="@+id/button02"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止Service" />    <Button        android:id="@+id/button03"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绑定Service" />    <Button        android:id="@+id/button04"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="解绑Service" /></LinearLayout>

5、Android Menifest.xml中添加Service的注册

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

6、打印输出


0 0
原创粉丝点击