AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
static {
......
// 将AudioManager加入SYSTEM_SERVICE_MAP中,调用getSystemService时,
// 就会从SYSTEM_SERVICE_MAP得到AudioManager
registerService(AUDIO_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
return new AudioManager(ctx);
}});
......
}
private static IAudioService getService()
{
if (sService != null) {
return sService;
}
// 从ServiceManager中获取Binder
IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
// 将Binder转化成IAudioService,方便调用
sService = IAudioService.Stub.asInterface(b);
return sService;
}
package com.test.lib;
interface IMyService {
void setValue(int val);
int getValue();
}
package com.test.myservice;
import android.os.RemoteException;
import com.test.lib.IMyService;
public class MyService extends IMyService.Stub {
private int value;
@Override
public void setValue(int val) throws RemoteException {
this.value = val;
}
@Override
public int getValue() throws RemoteException {
return value;
}
}
package com.test.myservice;
import android.app.Application;
import android.os.ServiceManager;
public class MyServiceApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
ServiceManager.addService("MYSERVICE", new MyService());
}
}
<application
android:name=".MyServiceApplication" //指定Application为我们创建的MyServiceApplication
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:persistent="true" // 加上persistent=ture,ActivityManager创建的时候,就会创建该应用的进程,并调用MyServiceApplication的onCreate方法
android:label="@string/app_name"
android:theme="@style/AppTheme" >
package com.test.lib;
import android.os.RemoteException;
import android.os.ServiceManager;
public class MyManager {
private static MyManager instance;
private IMyService myservice;
public static MyManager getInstance() {
if (instance == null) {
instance = new MyManager();
}
return instance;
}
private MyManager() {
// 从ServiceManager中获取服务
myservice = IMyService.Stub.asInterface(ServiceManager.getService("MYSERVICE"));
}
public void setValue(int value) throws RemoteException {
myservice.setValue(value);
}
public int getValue() throws RemoteException {
return myservice.getValue();
}
}
package com.test.client;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.test.binder.client.R;
import com.test.lib.MyManager;
public class MainActivity extends Activity implements OnClickListener {
MyManager myManager;
Button btnSetValue;
Button btnGetValue;
TextView tvValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
setContentView(R.layout.activity_main);
btnSetValue = (Button) findViewById(R.id.btn_set_value);
btnGetValue = (Button) findViewById(R.id.btn_get_value);
tvValue = (TextView) findViewById(R.id.tv_value);
// 获取MyManager
myManager = MyManager.getInstance();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_set_value:
int value = new Random().nextInt();
try {
myManager.setValue(value);
Toast.makeText(this, "set value to "+value+ " success!", 0).show();
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(this, "set value fail!", 0).show();
}
break;
case R.id.btn_get_value:
try {
tvValue.setText("value:"+myManager.getValue());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有