package com.jereh.remote;
interface LocalService{
String getLocal();
}
public class MyRemoteService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new MyRemoteServiceImpl();
}
private class MyRemoteServiceImpl extends LocalService.Stub{
@Override
public String getLocal() throws RemoteException {
// TODO Auto-generated method stub
return "烟台杰瑞教育";
}
}
}
<service android:name="com.jereh.retmote.MyRemoteService"
android:process="remote"
>
<intent-filter>
<action android:name="com.jereh.remote_service"/>
</intent-filter>
</service>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view){
Intent service=new Intent("com.jereh.remote_service");
super.bindService(service, conn, Context.BIND_AUTO_CREATE);
}
public void showInfo(View view){
try {
local=service.getLocal();
Log.d("jereh", local);
Toast.makeText(this,
"您已进入"+local,Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private LocalService service;
private String local;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
// TODO Auto-generated method stub
// 获取远程Service的onBinder方法返回的对象代理
service=LocalService.Stub.asInterface(binder);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动远程服务" android:onClick="startService" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看信息" android:onClick="showInfo" />
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有