访问本地服务里面的方法

原理图:



步骤:

调用服务里面的方法:
1 设计一个接口:IStudentQueryService   Student queryStudent(int no);
2 在activity里面进行绑定操作:
  bindService(intent,conn,flag);
3 写一个连接实现类:
    private final class MyServiceConnection implements ServiceConnection{


public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ibinder = (IStudnetQueryService) service;
}


public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
conn = null;
ibinder = null;
}
   
    }
4 在activity里面用IStudentQueryService 来接收onServiceConnected(ComponentName name, IBinder service)
  返回过来的IBinder对象。


5 写一个StudentService  extends  Service  ,重写onBind()
  编译一个内部类StudentBinder extends Binder  implements IStudnetQueryService


6  创建StudentBinder的对象,通过onBind()方法返回。
7  在activity通过onBind()返回的对象调用服务里面的方法。



代码如下:

1、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="学号"
        />
    
    <EditText 
        android:id="@+id/et_no"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:onClick="query1"
        />
    
    <TextView  
        android:id="@+id/tv_info"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    
</LinearLayout>


2、MainActivity

package com.njupt.studentquery1;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private EditText et_no;
	private TextView tv_info;
	private MyServiceConnection conn;
	private IStudentQueryService ibinder;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	    
		et_no = (EditText) findViewById(R.id.et_no);
		tv_info = (TextView) findViewById(R.id.tv_info);
		conn = new MyServiceConnection();
		
		Intent intent = new Intent(this,StudentService.class);
		bindService(intent,conn,BIND_AUTO_CREATE);
	}

	private class MyServiceConnection implements ServiceConnection{
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			ibinder = (IStudentQueryService) service;
		}
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			ibinder = null;
			conn = null;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    
		unbindService(conn);
	}
	
	public void query1(View v){
		String no = et_no.getText().toString();
		Student student = ibinder.queryStudent(Integer.parseInt(no));
		tv_info.setText(student.toString());
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


3、StudentService

package com.njupt.studentquery1;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class StudentService extends Service {

	private StudentBinder ibinder = new StudentBinder();
	private Student[] students = new Student[]{
		new Student(1,"章泽天",21),
		new Student(2,"刘诗诗",22),
		new Student(3,"allen",21)
	};
	
	@Override
	public IBinder onBind(Intent intent) {
		return ibinder;
	}
	
	private class StudentBinder extends Binder implements IStudentQueryService{
		public Student queryStudent(int no){
			return query(no);
		}
	}

	public Student query(int no){
		return students[no - 1];
	}
	
}


4、IStudentQueryService

package com.njupt.studentquery1;

public interface IStudentQueryService {

	public Student queryStudent(int no);
}


5、Student

package com.njupt.studentquery1;

public class Student {

	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	
}


6、AndroidManifest.xml

记得在清单文件中吧service 给注册上去

<service android:name="com.njupt.studentquery1.StudentService"/>


### 如何配置服务器只允许本地主机访问 #### Nuxt.js 项目中的本地主机访问限制 对于基于 Nuxt.js 的开发环境,默认情况下,应用会监听 `http://localhost:3000` 或者所有可用接口上的某个端口。为了确保只有来自同一台机器的请求能够到达应用程序,可以通过调整启动命令来限定绑定地址为 `127.0.0.1` 而不是 `0.0.0.0`。 当使用 npx 或 npm run dev 启动 Nuxt 应用时,可以传递 host 参数给它以指定要绑定的具体 IP 地址: ```bash npm run dev -- --host 127.0.0.1 ``` 这将使得该 Web 服务仅响应来自于 localhost (即本机) 的连接尝试[^1]。 #### Linux 系统下的防火墙规则设置 在Linux操作系统上,如果希望进一步加强安全性并阻止外部网络流量进入,则可以在 iptables 中添加相应规则。下面是一个简单的例子,用于创建一条新链并将除回环设备外的所有入站数据包丢弃: ```bash sudo iptables -A INPUT ! -i lo -j DROP ``` 此命令表示除了源自 loopback 接口的数据包之外,其他任何试图进入系统的 TCP/IP 数据都将被拒绝处理。请注意,在执行此类操作之前应当充分理解其影响范围,并考虑是否有更合适的方式达成目标[^4]。 #### Windows Server 上的应用程序级控制 针对 Windows 平台而言,可通过内置的安全中心或高级安全 Windows Defender 防火墙来进行更为精细的权限管理。创建一个新的入站规则,选择“按程序”选项卡之后定位到 node.exe (假设正在运行的是 Node.js/Nuxt),接着设定条件只为那些源地址匹配 %SystemRoot%\System32\drivers\etc\hosts 文件里定义过的名称解析成 127.0.0.1 的情况放行。 另一种方法是在 IIS(Internet Information Services)中部署网站时,编辑站点属性里的 "Bindings" 设置项,把 Binding Type 设定为 HTTP 和特定 IPv4 地址 127.0.0.1 组合的形式[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值