绑定本地服务,得到服务对象为空,求解答
服务:
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>
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>
...全文
相关推荐
C++面向对象程序设计教程习题及解答
**封装**:封装是面向对象的核心特性之一,它将数据和操作数据的方法绑定在一起,隐藏了实现细节,只对外提供公共接口。C++通过访问修饰符(public、private、protected)实现封装。
基于java的本地代码绑定工具 jSegue.zip
Java本地代码绑定工具jSegue是一个强大的开发工具,主要用于在Java应用程序中无缝地集成本地(C或C++)代码。
c++面向对象程序设计课后习题解答
以上只是C++面向对象编程的一部分基础知识。在《C++面向对象程序设计课后习题解答》这本书中,读者可以找到更具体的实践问题,通过解答这些习题,深入理解并掌握C++的面向对象编程技巧。
C++面向对象程序设计[姚全珠][习题解答]
《C++面向对象程序设计[姚全珠][习题解答]》是一本专注于C++编程语言面向对象特性的学习资源,特别关注了如何通过解决实际问题来深化对这一概念的理解。
路由器ip与MAC地址可以不设置绑定吗详细解答
总的来说,尽管不绑定IP和MAC地址的路由器仍然能够提供基本的网络服务,但在网吧这样的环境中,为了确保网络安全和稳定,绑定IP与MAC地址是必要的措施。


