我已经创建了类似的功能
看看对你有没有帮助
您需要获得使用 Internet 的权限。在清单文件中,在应用程序标签之外声明互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
创建一个活动(WebViewActivity.java)
public class WebViewActivity extends AppCompatActivity {
private static final String TAG = "WebViewActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Log.d(TAG, "onCreate: Started");
Bundle extras = getIntent().getExtras();
assert extras != null;
String pin = extras.getString("pin"); //you can use getInt("pin") also, but afterwards in the url, it will be converted to String back, so it is not needed
String status = extras.getString("status"); //you can use getInt("status") also, but afterwards in the url, it will be converted to String back, so it is not needed
WebView webView = new WebView(this);
setContentView(webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); //This is required to enable javascript that is required by some pages
String url = "http://192.168.0.105/index.php?pin" + pin + "&status=" + status;
webView.loadUrl(url);
Log.d(TAG, "url accessed: " + url);
finish();//If you need to see the output after accessing the http page, then remove this line. Otherwise just copy as it is
}
}
在MainActivity中,启动上面创建的activity即可。
public class MainActivity extends AppCompatActivity{
int pin = 0, status = 2;// You can modify the values according to your requirements
//....
//....
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
intent.putExtra("pin", pin);
intent.putExtra("status", status);
startActivity(intent);
Toast.makeText(MainActivity.this, "URL Opened and pin, status values are successfully updated", Toast.LENGTH_SHORT).show();
}
});
//....
//....
}
如果您想知道在 xml 文件中放入什么 (activity_web_view.xml)
保持原样,我使用下面的代码发送值,就像你试图发送一样。
activity_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebViewActivity" />