绑定本地服务,得到服务对象为空,求解答

蓝天&前行 2012-10-30 11:22:15
服务:

public class MyService extends Service {
private MyBind mBind = new MyBind();

@Override
public IBinder onBind(Intent intent) {

return mBind;
}

public String mCallMethod() {
return "我被调用了";
}

class MyBind extends Binder implements IService {
@Override
public String reciveMessage() {
String str = mCallMethod();
return str;
}
}
}

服务调用者:

public class ServiceDemoActivity extends Activity {

private IService mService;
private Button call;
private TextView dis;
private Button bind;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
call = (Button) findViewById(R.id.caller);
bind=(Button)findViewById(R.id.bind);
bind.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
bindService();

}
});
dis = (TextView) findViewById(R.id.content);
bindService();
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str=mService.reciveMessage();
dis.setText(str);
}
});

}

public void bindService(){
bindService(new Intent(this, MyService.class), new MyConnection(), BIND_AUTO_CREATE);
}

public void unBindService(){
unbindService(new MyConnection());
}

class MyConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService=(IService)service;
}

@Override
public void onServiceDisconnected(ComponentName name) {

}

}
}

main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button android:id="@+id/bind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
/>

<Button android:id="@+id/caller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="调用服务"
/>


<TextView android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:text=""
android:gravity="center"
android:layout_marginTop="30dip"
android:layout_gravity="center"
/>

</LinearLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service.demo"
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=".ServiceDemoActivity"
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=".MyService" />

</application>
</manifest>
...全文
83 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 Altium Designer 18是一款功能丰富的电子设计自动化工具,其集成了电路原理图绘制、PCB布局规划、三维视图展示、电路仿真分析以及生产文件生成等多项核心功能,为电子工程师们提供了一个全面的硬件设计解决方案。该软件的官方中文指导材料系统地阐述了从项目初始阶段到最终完成的全过程操作方法。 在“AD18 官方中文指导材料”中,使用者可以掌握以下核心内容要点: 1. **系统配置与界面认知**:熟悉Altium Designer 18的工作平台,涵盖菜单选项、工具栏配置、工作区域安排,以及个性化工作界面的设定方法。 2. **项目创建流程**:掌握如何建立新的项目工程,包括工程参数配置、项目模板选取,以及项目文件的添加操作。 3. **电路图绘制技术**:学习电路原理图的绘制流程,包括元件库的维护管理、元件放置技巧、连接线路绘制、属性编辑操作,以及网络表的自动生成方法。 4. **元件库构建与管理**:了解如何建立自定义元件库,执行元件的导入与导出任务,以及利用Altium Designer自带的元件资源库。 5. **PCB布局设计方法**:掌握PCB设计的基本准则,如元件布局策略、布线技巧、层叠结构调整、布线规则优化,以及冲突检测与短路处理技术。 6. **设计规范与约束条件**:理解设定设计规范和电气约束条件的关键性,包括间距参数设定、焊盘尺寸规格、过孔尺寸要求等,以及运用规则检查进行设计验证的流程。 7. **三维模型整合应用**:学习如何将三维模型与PCB设计内容相结合,以实现更为直观的机械配合验证。 8. **电路性能仿真技术**:掌握使用Altium Des...

80,488

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧