Android_08_创建,启动和关闭远程服务
2015-10-21 23:37
417 查看
前言:
服务的分类:
本地服务:指的是服务和启动服务的activity在同一个进程中
远程服务:指的是服务和启动服务的activity不在同一个进程中
创建并启动远程服务的代码示例如下:
创建远程服务:
MainActivity.java
package com.itheima.remoteservice;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
RemoteService.java
package com.itheima.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Bind方法");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Create方法");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("StartCommand方法");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Destroy方法");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Unbind方法");
return super.onUnbind(intent);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
文件清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.remoteservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.remoteservice.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="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
</application>
</manifest>
注:在创建远程服务时,其与本地服务的区别之处在于清单文件的配置,
在配置本地服务的清单文件时,仅仅只需声明:<service android:name="com.itheima.remoteservice.RemoteService"></service>在配置远程服务的清单文件时,除了需呀声明service节点之外,还需要在
intent-filter节点中声明action子节点,如:
<service android:name="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
对于远程服务,声明action是为了用于去启动远程服务
启动远程服务:
MainActivity.java
package com.itheima.runremote;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private MyserviceConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new MyserviceConn();
}
public void click(View v){
//启动远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
startService(intent);
}
public void click2(View v){
//停止远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
stopService(intent);
}
public void click3(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void click4(View v){
unbindService(conn);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}
activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动远程服务"
android:onClick="click"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止远程服务"
android:onClick="click2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定远程服务"
android:onClick="click3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑远程服务"
android:onClick="click4"
/>
</LinearLayout>
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.runremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.runremote.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>
</application>
</manifest>
注:在启动远程服务上,其实现步骤和启动本地服务类似,
只是需要额外地设置一下意图的动作,如:
intent.setAction("com.itheima.remote");
因为在远程服务的清单文件中设置了action节点
所以对应的当然需要在意图上对其进行设置;
服务的分类:
本地服务:指的是服务和启动服务的activity在同一个进程中
远程服务:指的是服务和启动服务的activity不在同一个进程中
创建并启动远程服务的代码示例如下:
创建远程服务:
MainActivity.java
package com.itheima.remoteservice;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
RemoteService.java
package com.itheima.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Bind方法");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Create方法");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("StartCommand方法");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Destroy方法");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Unbind方法");
return super.onUnbind(intent);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
文件清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.remoteservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.remoteservice.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="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
</application>
</manifest>
注:在创建远程服务时,其与本地服务的区别之处在于清单文件的配置,
在配置本地服务的清单文件时,仅仅只需声明:<service android:name="com.itheima.remoteservice.RemoteService"></service>在配置远程服务的清单文件时,除了需呀声明service节点之外,还需要在
intent-filter节点中声明action子节点,如:
<service android:name="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
对于远程服务,声明action是为了用于去启动远程服务
启动远程服务:
MainActivity.java
package com.itheima.runremote;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private MyserviceConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new MyserviceConn();
}
public void click(View v){
//启动远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
startService(intent);
}
public void click2(View v){
//停止远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
stopService(intent);
}
public void click3(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void click4(View v){
unbindService(conn);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}
activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动远程服务"
android:onClick="click"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止远程服务"
android:onClick="click2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定远程服务"
android:onClick="click3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑远程服务"
android:onClick="click4"
/>
</LinearLayout>
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.runremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.runremote.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>
</application>
</manifest>
注:在启动远程服务上,其实现步骤和启动本地服务类似,
只是需要额外地设置一下意图的动作,如:
intent.setAction("com.itheima.remote");
因为在远程服务的清单文件中设置了action节点
所以对应的当然需要在意图上对其进行设置;
相关文章推荐
- 深入理解Android之Gradle
- android Animation介绍
- 【MDCC 2015】开源选型之Android三大图片缓存原理、特性对比
- Android中级篇之百度地图SDK v3.5.0-百度地图定位[超详细图解定位基础]
- android.os.networkonmainthreadexception
- Android实现AirPlay,DriodAirPlay开发
- Under the Hood: Dalvik patch for Facebook for Android
- [转]Mac OS X 下部分Android手机无法连接adb问题之解决方案
- 学习Android(3)
- Android酷炫实用的开源框架(UI框架)
- 正确使用Android性能分析工具——TraceView
- Android系统性能调优工具介绍
- Android_左右滑动切换背景
- Android程序怎样禁止横竖屏切换
- android中Fragment的构造函数
- Android中保存和恢复Fragment状态的最好方法-1
- Android_08_使用服务注册广播
- 从问题到解决方案到应用-android-ApiDemo入口源代码学习及应用
- Google《Android性能优化》大全
- 【Android杂谈】安卓开发必需了解 -AIDL(一)