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

蓝天&前行 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>
...全文
36 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,354

社区成员

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

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