当前位置 : 主页 > 网络编程 > PHP >

Android banZheng

来源:互联网 收集:自由互联 发布时间:2023-10-08
public class MainActivity extends AppCompatActivity { private BanZhengService . MyBinder myBinder ; // 我定义的中间人对象 private MyConn conn ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( sa


public class MainActivity extends AppCompatActivity {

private BanZhengService.MyBinder myBinder; // 我定义的中间人对象
private MyConn conn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent intent = new Intent(this, BanZhengService.class);
//连接服务
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
//点击按钮调用服务里面banzheng的方法
public void click(View v) {
myBinder.callBanZheng(10);
}

//监视服务的状态
private class MyConn implements ServiceConnection {
//当服务连接成功时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//获取中间人对象
myBinder = (BanZhengService.MyBinder) service;
}
//失去连接
@Override
public void onServiceDisconnected(ComponentName name) {

}
}

@Override
protected void onDestroy() {
//当activity销毁的时候 解绑服务
unbindService(conn);
super.onDestroy();
}
}


public class BanZhengService extends Service {
//把我定义的中间人对象返回
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new MyBinder();
}

public void banZheng(int money) {
if (money>1000) {
Toast.makeText(getApplicationContext(), "我是lingdao,把zheng给你办了", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "qianbugou", Toast.LENGTH_SHORT).show();
}
}

//定义中间人对象(IBinder)
public class MyBinder extends Binder {
public void callBanZheng(int money) {
//调用banzheng的方法
banZheng(money);
}
}
}




【文章转自 东台网页设计 http://www.1234xp.com/dongtai.html 提供,感恩】
网友评论