您的位置:首页 > 移动开发 > Android开发

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节点

所以对应的当然需要在意图上对其进行设置;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: