APP: Android Studio v.2020.3.1.26 Patch4, published at December 8, 2021 (https://developer.android.com/studio/archive) please note: Change to English language or may not find this version language: JAVA Minimum SDK platform: Android 9 (pie)
//in .java TextView your_view_name=findViewById(R.id.your_view_id); // find target textView String message=your_view_name.getText().toString(); // get written string in this EditText your_view_name.setText(message); // input message to textView
@Override //get value public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.%sensorType%) //target sensor type sensorName = event.values; }
//varibale to hold BluetoothAdapter instance private BluetoothAdapter BA; //display list of paired Bluetooth device private Set<BluetoothDevice> pairedDevices;
BA = BluetoothAdapter.getDefaultAdapter();
//get list pairedDevices = BA.getBondedDevices();
Location
note the “onLocationChanged” will not be changes swiftly
//be called when Map page is initialized ready public void onMapReady(GoogleMap googleMap) { mMap = googleMap;
// Add a marker in Sydney and move the camera LatLng Edinburgh = new LatLng(55.953251, -3.188267); //add new markers mMap.addMarker(new MarkerOptions().position(Edinburgh).title("Marker in Edinburgh")); //move the center of camera to marker mMap.moveCamera(CameraUpdateFactory.newLatLng(Edinburgh));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the casewhere the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } //enable the location button, move the sign to your current place mMap.setMyLocationEnabled(true);
//in main class intent intent=new Intent(this,New_class.class); // create an intent class which will communicated with new class intent.putExtra('extras_name', 'your_message'); // put the string into EXTRA_MESSAGE. startActivity (intent); // running intent
//in new class Intent intent=getIntent(); string message=intent.getStringExtra('extras_name');// message will get string value //you can use intent.getIntExtra('extras_name') to get integer value
read battery states
1 2 3 4 5 6 7 8 9 10 11
private IntentFilter ifilter; //in OnCreate ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); //ifilter have matched with a type of action registerReceiver(mBatInforReceiver,ifilter); //the receiver will be called when any broadcast matches filter
if (Build.VERSION.SDK_INT >= 23) { //check if we have read or write permission int myPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.XXXX); }
if (myPermission != PackageManager.PERMISSION_GRANTED) { //prompt the user if dont have permission this.requestPermissions( new String[]{Manifest.permission.XXXX}, REQUEST_ID_READ_WRITE_PERMISSION ); return; } }
//When you have the request results @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_ID_READ_WRITE_PERMISSION: { //(read and write and camera) permissions granted if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Permission granted!", Toast.LENGTH_LONG).show(); } //cancelled or denied else { Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show(); } break; } } }
tips
linked Strings
in res -> values -> strings.xml, we can set some default variable strings, in form like