- 代码
- package com.amaker.ch07.app;
- import com.amaker.ch07.app.R;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- 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;
- /**
- * 测试Service
- */
- public class MainActivity extends Activity {
- // 声明Button
- private Button startBtn,stopBtn,bindBtn,unbindBtn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置当前布局视图
- setContentView(R.layout.main);
- // 实例化Button
- startBtn = (Button)findViewById(R.id.startButton01);
- stopBtn = (Button)findViewById(R.id.stopButton02);
- bindBtn = (Button)findViewById(R.id.bindButton03);
- unbindBtn = (Button)findViewById(R.id.unbindButton04);
- // 添加监听器
- startBtn.setOnClickListener(startListener);
- stopBtn.setOnClickListener(stopListener);
- bindBtn.setOnClickListener(bindListener);
- unbindBtn.setOnClickListener(unBindListener);
- }
- // 启动Service监听器
- private OnClickListener startListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Action属性
- intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
- // 启动该Service
- startService(intent);
- }
- };
- // 停止Service监听器
- private OnClickListener stopListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Action属性
- intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
- // 启动该Service
- stopService(intent);
- }
- };
- // 连接对象
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.i("SERVICE", "连接成功!");
- Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.i("SERVICE", "断开连接!");
- Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG).show();
- }
- };
- // 綁定Service监听器
- private OnClickListener bindListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Action属性
- intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
- // 绑定Service
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- }
- };
- // 解除绑定Service监听器
- private OnClickListener unBindListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建Intent
- Intent intent = new Intent();
- // 设置Action属性
- intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
- // 解除绑定Service
- unbindService(conn);
- }
- };
- }
/Chapter07_Service_Example/src/com/amaker/ch07/app/MyService.java
- 代码
- package com.amaker.ch07.app;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.widget.Toast;
- /**
- * 测试Service
- */
- public class MyService extends Service{
- // 可以返回null,通常返回一个有aidl定义的接口
- public IBinder onBind(Intent intent) {
- Log.i("SERVICE", "onBind..............");
- Toast.makeText(MyService.this, "onBind..............", Toast.LENGTH_LONG).show();
- return null;
- }
- // Service创建时调用
- public void onCreate() {
- Log.i("SERVICE", "onCreate..............");
- Toast.makeText(MyService.this, "onCreate..............", Toast.LENGTH_LONG).show();
- }
- // 当客户端调用startService()方法启动Service时,该方法被调用
- public void onStart(Intent intent, int startId) {
- Log.i("SERVICE", "onStart..............");
- Toast.makeText(MyService.this, "onStart..............", Toast.LENGTH_LONG).show();
- }
- // 当Service不再使用时调用
- public void onDestroy() {
- Log.i("SERVICE", "onDestroy..............");
- Toast.makeText(MyService.this, "onDestroy..............", Toast.LENGTH_LONG).show();
- }
- }
布局文件
/Chapter07_Service_Example/res/layout/main.xml
- 代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="启动Service"
- android:id="@+id/startButton01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- <Button
- android:text="停止Service"
- android:id="@+id/stopButton02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- <Button
- android:text="绑定Service"
- android:id="@+id/bindButton03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- <Button
- android:text="解除绑定"
- android:id="@+id/unbindButton04"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- </LinearLayout>
清单文件
/Chapter07_Service_Example/AndroidManifest.xml
- 代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.amaker.ch07.app"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name="MyService">
- <intent-filter>
- <action android:name="com.amaker.ch07.app.action.MY_SERVICE"/>
- </intent-filter>
- </service>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
实例二、远程service调用
实现方式RPC(remote procedures call)远程进程调用 (android interface definition)接口定义语言
/Chapter07_Service_Remote/src/com/amaker/ch07/app/MainActivity.java
- 代码
- package com.amaker.ch07.app;
- import com.amaker.ch07.app.IPerson;
- import com.amaker.ch07.app.R;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- /**
- *
- * RPC 测试
- */
- public class MainActivity extends Activity {
- // 声明IPerson接口
- private IPerson iPerson;
- // 声明 Button
- private Button btn;
- // 实例化ServiceConnection
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- synchronized public void onServiceConnected(ComponentName name, IBinder service) {
- // 获得IPerson接口
- iPerson = IPerson.Stub.asInterface(service);
- if (iPerson != null)
- try {
- // RPC 方法调用
- iPerson.setName("hz.guo");
- iPerson.setAge(30);
- String msg = iPerson.display();
- // 显示方法调用返回值
- Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG)
- .show();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置当前视图布局
- setContentView(R.layout.main);
- // 实例化Button
- btn = (Button) findViewById(R.id.Button01);
- //为Button添加单击事件监听器
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 实例化Intent
- Intent intent = new Intent();
- // 设置Intent Action 属性
- intent
- .setAction("com.amaker.ch09.app.action.MY_REMOTE_SERVICE");
- // 绑定服务
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- }
- });
- }
- }
/Chapter07_Service_Remote/src/com/amaker/ch07/app/MyRemoteService.java
- 代码
- package com.amaker.ch07.app;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import com.amaker.ch07.app.IPerson.Stub;
- /**
- * 使用Service将接口暴露给客户端
- */
- public class MyRemoteService extends Service{
- // 声明IPerson接口
- private Stub iPerson = new IPersonImpl();
- @Override
- public IBinder onBind(Intent intent) {
- return iPerson;
- }
- }
/Chapter07_Service_Remote/src/com/amaker/ch07/app/IPersonImpl.java
- 代码
- package com.amaker.ch07.app;
- import com.amaker.ch07.app.IPerson;
- import android.os.RemoteException;
- /**
- *
- * IPerson接口实现类
- */
- public class IPersonImpl extends IPerson.Stub{
- // 声明两个变量
- private int age;
- private String name;
- @Override
- // 显示name和age
- public String display() throws RemoteException {
- return "name:"+name+";age="+age;
- }
- @Override
- // 设置age
- public void setAge(int age) throws RemoteException {
- this.age = age;
- }
- @Override
- // 设置name
- public void setName(String name) throws RemoteException {
- this.name = name;
- }
- }
/Chapter07_Service_Remote/src/com/amaker/ch07/app/IPerson.aidl
- package com.amaker.ch07.app;
- interface IPerson {
- void setAge(int age);
- void setName(String name);
- String display();
- }
布局文件
/Chapter07_Service_Remote/res/layout/main.xml
- 代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="测试远程Service"
- android:id="@+id/Button01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- </LinearLayout>
清单文件
/Chapter07_Service_Remote/AndroidManifest.xml
- 代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.amaker.ch07.app"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name="MyRemoteService">
- <intent-filter>
- <action android:name="com.amaker.ch07.app.action.MY_REMOTE_SERVICE"/>
- </intent-filter>
- </service>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
转载于:https://blog.51cto.com/linzheng/1080655