大家好,又见面了,我是你们的朋友全栈君。
public class MainActivity extends Activity {
private MBind mBind;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 绑定服务
mBind = (MyService.MBind) service;
}
};
// 开启服务
bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
}
//Service里面的代码
public class MyService extends Service {
private MBind mBind;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mBind = new MBind();
}
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBind;
}
//建立自定义的类 继承Binder
public class MBind extends Binder {
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/138582.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...