<service android:name="MyRemoteService"> <intent-filter> <action android:name="cn.com.chenzheng_java.remote"/> </intent-filter> </service>
package cn.com.chenzheng_java.remote;
/**AIDL的语法和Interface的语法稍微有些不同,
*它里面只能有方法,并且java中的那些修饰词如public等,在这里是不支持的.
*当我们在eclipse中添加一个以.aidl结尾的AIDL文件时,如果你的格式正确,那么
*在gen目录下,你就会看到系统根据你提供AIDL文件自动为你生成的相应的java类
*@author chenzheng_java
*/
interface RemoteServiceInterface {
void startMusic();
void stopMusic();
}
package cn.com.chenzheng_java.remote;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
/**
* @description 远程service
* @author chenzheng_java
*
*/
public class MyRemoteService extends Service {
MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
/*
* MediaPlayer.create方法第一个参数实际上为context对象,这里我们直接传递service给它,
* 是因为service本身也是继承了context的。
*/
mediaPlayer = MediaPlayer.create(MyRemoteService.this, R.raw.aiweier);
super.onCreate();
}
/**
* @description 这里一定要注意,继承的不再是Binder,而是系统自动为我们生成的
* Binder的一个内部类,叫做Stub。我们通过继承Stub类,然后实现AIDL
* 中定义的方法,便等于对接口的方法进行了具体的实现。
* @author chenzheng_java
*
*/
private class MyBinder extends RemoteServiceInterface.Stub{
public void startMusic() throws RemoteException {
mediaPlayer.start();
}
public void stopMusic() throws RemoteException {
mediaPlayer.stop();
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package cn.com.chenzheng_java.remote;
import android.app.Activity;
import android.os.Bundle;
/**
* 很多人不理解,这里面基本上什么也没实现,为什么还要提供呢?
* 其实原因是这样的,service代码我们写好了,也在配置文件中注册了,
* 这样,手机系统就会识别了吗?不是的哦,你至少得将该service所在的
* 应用运行一次才可以哦。要不手机怎么知道你添加了一个service啊,对吧!
* @author chenzheng_Java
*
*/
public class RemoteServiceActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.chenzheng_java.remote"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".RemoteServiceActivity"
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="cn.com.chenzheng_java.remote"/>
</intent-filter>
</service>
</application>
</manifest>
package cn.com.chenzheng_java;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.com.chenzheng_java.remote.RemoteServiceInterface;
/***
* @author chenzheng_java
* @description 通过当前activity去调用不同进程中的远程service
*/
public class LocalServiceActivity extends Activity implements OnClickListener {
String ACTION_NAME = "cn.com.chenzheng_java.remote";
boolean flag = false;
Button button_start ;
Button button_stop ;
Button button_destroy ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
button_start = (Button) findViewById(R.id.button1);
button_stop = (Button) findViewById(R.id.button2);
button_destroy = (Button) findViewById(R.id.button3);
button_start.setOnClickListener(this);
button_stop.setOnClickListener(this);
button_destroy.setOnClickListener(this);
}
RemoteServiceInterface remoteServiceInterface ;
private class MyServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
remoteServiceInterface = RemoteServiceInterface.Stub.asInterface(service);
try {
Log.i("flag", flag+"");
if(flag){
Log.i("通知", "已经开始唱歌");
remoteServiceInterface.startMusic();
}else{
Log.i("通知", "已经停止唱歌");
remoteServiceInterface.stopMusic();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName name) {
}
}
private MyServiceConnection serviceConnection = new MyServiceConnection();
public void onClick(View v) {
if(v == button_start){
flag = true;
Intent intent = new Intent(ACTION_NAME);
/**
* Context.BIND_AUTO_CREATE 当绑定service时,如果发现尚未create,那么就先create一个,然后绑定
*/
bindService(intent, serviceConnection ,Context.BIND_AUTO_CREATE);
}
if(v == button_stop){
Log.i("通知", "已经点击了停止按钮");
flag = false;
Intent intent = new Intent(ACTION_NAME);
bindService(intent, serviceConnection ,Context.BIND_AUTO_CREATE);
try {
remoteServiceInterface.stopMusic();
} catch (RemoteException e) {
e.printStackTrace();
}
}
if(v == button_destroy){
flag = false;
Intent intent = new Intent(ACTION_NAME);
bindService(intent, serviceConnection ,Context.BIND_AUTO_CREATE);
unbindService(serviceConnection);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="播放" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="停止" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="销毁service" android:id="@+id/button3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
package cn.com.chenzheng_java.service;
import android.os.Parcel;
import android.os.Parcelable;
/**
*
* @author chenzheng_java
* @description Parcelable是android提供的一个比serializable效率更高的序列号接口
* 这里必须要继承Parcelable哦,不序列号怎么可以传递……对吧?!
* 在实体类我们要做两件重要的事情:
* 第一:实现Parcelable接口
* 第二:定义一个Parcelable.Creator类型的CREATOR对象
* 第三:要提供一个Beauty.aidl文件,其中内容为parcelable Beauty,定义了之后,在其他aidl文件中引用Beauty时便不会提示出错了。
* @since 2011/03/18
*
*/
public class Beauty implements Parcelable {
String name ;
int age ;
String sex ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public int describeContents() {
return 0;
}
/**
* 将对象序列号
* dest 就是对象即将写入的目的对象
* flags 有关对象序列号的方式的标识
* 这里要注意,写入的顺序要和在createFromParcel方法中读出的顺序完全相同。例如这里先写入的为name,
* 那么在createFromParcel就要先读name
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(sex);
}
/**
* 在想要进行序列号传递的实体类内部一定要声明该常量。常量名只能是CREATOR,类型也必须是
* Parcelable.Creator<T>
*/
public static final Parcelable.Creator<Beauty> CREATOR = new Creator<Beauty>() {
/**
* 创建一个要序列号的实体类的数组,数组中存储的都设置为null
*/
@Override
public Beauty[] newArray(int size) {
return new Beauty[size];
}
/***
* 根据序列号的Parcel对象,反序列号为原本的实体对象
* 读出顺序要和writeToParcel的写入顺序相同
*/
@Override
public Beauty createFromParcel(Parcel source) {
String name = source.readString();
int age = source.readInt();
String sex = source.readString();
Beauty beauty = new Beauty();
beauty.setName(name);
beauty.setAge(age);
beauty.setSex(sex);
return beauty;
}
};
}
package cn.com.chenzheng_java.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
*
* @author chenzheng_java
* @description 提供服务的service
*
*/
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.i("通知", "执行了OnBind");
return new MyBinder();
}
private class MyBinder extends RemoteBeauty.Stub{
@Override
public Beauty getBeauty() throws RemoteException {
Beauty beauty = new Beauty();
beauty.setName("feifei");
beauty.setAge(21);
beauty.setSex("female");
return beauty;
}}
}
package cn.com.chenzheng_java.service;
import android.app.Activity;
import android.os.Bundle;
/**
* @description 进程之间对象数据的传递
* @author chenzheng_java
*
*/
public class ServiceActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
parcelable Beauty;
RemoteBeauty.aidl
package cn.com.chenzheng_java.service;
import cn.com.chenzheng_java.service.Beauty;
interface RemoteBeauty {
Beauty getBeauty();
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.chenzheng_java.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ServiceActivity"
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开始 -->
<service android:name="RemoteService">
<intent-filter>
<action android:name="cn.com.chenzheng_java.remote2"/>
</intent-filter>
</service>
<!-- service结束 -->
</application>
</manifest>
package cn.com.chenzheng_java.client;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cn.com.chenzheng_java.service.Beauty;
import cn.com.chenzheng_java.service.RemoteBeauty;
public class ClientActivity extends Activity implements OnClickListener {
TextView textView ;
Button button ;
String actionName = "cn.com.chenzheng_java.remote2";
RemoteBeauty remoteBeauty;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
private class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("通知", "链接成功!");
remoteBeauty = RemoteBeauty.Stub.asInterface(service);
try {
Beauty beauty = remoteBeauty.getBeauty();
textView.setText("美女 姓名:"+beauty.getName()+" 年龄:"+beauty.getAge() +" 性别:"+beauty.getSex());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
MyServiceConnection connection = new MyServiceConnection();
@Override
public void onClick(View v) {
Intent intent = new Intent(actionName);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
}
<receiver android:name=".StartBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有