prioritise Async task over web view - android-studio

My android application has web-view and control buttons, web view streams live video from raspberry pi camera and on button press the traction commands(on,off,right,left) goes to the redis-DB on the same raspberry-pi
my observations is when web view streams video the control commands on button pressed reach pi at very slow speed
my priority is control first and then streaming how to accomplish the same?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://192.168.0.190:8000");
Log.e("aaa","Streaming video");
}
//on button clicked
public void StartTraction(View view) {
MyAsyncTaskStart myAsyncTasks = new MyAsyncTaskStart();
myAsyncTasks.execute();
}
public class MyAsyncTaskStart extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... strings) {
try {
Jedis jedis = new Jedis("192.168.0.190", 6379);
jedis.set("motorKey","ON");
jedis.disconnect();
} catch (JedisConnectionException e) {
} catch (NullPointerException e1) {
} catch (Exception e1) {
}
return "";
}
#Override
protected void onPostExecute(String s) {
}
}
}

Related

i need to get jpg from arduino host to android

So i need to get picture from host, arduino is host with the esp32 and the camera. Arduino is hosting picture on local wifi and I can access that picture by typing ip. Now I need help with the android studio part to get the image from the ip.
So this is the code from android studio and its not working.
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
new DownloadImageTask().execute("http://192.168.1.8/");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... urls) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urls[0])
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response.code());
}
byte[] bytes = response.body().bytes();
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}
}

Call Method Continuously When button in pressed state

public class MainActivity extends AppCompatActivity {
public ConsumerIrManager consumerIrManager;
public Vibrator v;
public int frequency = 38000;
public int t = 0;
public Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
consumerIrManager = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
v=(Vibrator) getSystemService(VIBRATOR_SERVICE);
}
i want to add a event on plus button when i pressed the button it works continuously till i am released button.
for Example :- Tv Remote Button when i press volume + button the volume increase continuously till releasing.
public void onClick(View view) {
if(!consumerIrManager.hasIrEmitter()) {
Toast.makeText(getApplicationContext(), "IR Emitter Not Found in Your Device", Toast.LENGTH_LONG).show();
return;
}
switch (view.getId()) {
case R.id.power:
v.vibrate(VibrationEffect.createOneShot(200,VibrationEffect.DEFAULT_AMPLITUDE));
if (t==0) {
Log.i("Button pressed", "Power");
// textt.setText("0");
for(i=0;i<3;i++) {
consumerIrManager.transmit(frequency,btn.power);
}
t=1;
}
else{
Log.i("Button pressed", "Power1");
consumerIrManager.transmit(frequency,btn.power0);
t=0;
}
break;
case R.id.plus:
v.vibrate(VibrationEffect.createOneShot(200,VibrationEffect.DEFAULT_AMPLITUDE));
if (t==0) {
consumerIrManager.transmit(frequency, btn.plus);
Log.i("Button pressed", "Plus");
t=1;
}
else{
consumerIrManager.transmit(frequency, btn.plus0);
strong text Log.i("Button pressed", "Plus1");
t=0;
}
break;

How to stop media player sound in view pager?

I am new to Android development and I have slider fragments in my activity. I am trying to play different sounds for different sliders. However, when I slide from one page to another page, the sounds are overlapping. Can anyone help us? Here's my code:
public class Diabetes1 extends Fragment {
MediaPlayer mp;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.slidertopimagecardlayout, container, false);
SharedPreferences prefs = this.getActivity().getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
if (prefs.getString("myLanguage", "").equals("en")) {
mp = MediaPlayer.create(getActivity().getBaseContext(), getResources().getIdentifier("diab1en", "raw", this.getActivity().getPackageName()));
mp.start();
} else if (prefs.getString("myLanguage", "").equals("es")) {
mp = MediaPlayer.create(getActivity().getBaseContext(), getResources().getIdentifier("diab1en", "raw", this.getActivity().getPackageName()));
mp.start();
}
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
// If we are becoming invisible, then...
if (!isVisibleToUser) {
mp.stop();
} else {
// do what you like
}
}
}
#Override
public void onPause() {
super.onPause();
if(mp.isPlaying()) {
mp.stop();
}
}
#Override
public void onResume() {
super.onResume();
}
}
Viewpager doesn't work appropriately for your purpose because it creates offscreen fragments for fast transition between pages. For your problem onCreateView is called for each but onPause isn't. To handle this you can use pageChangeListener with your pagerAdapter. After each page change you can switch to another sound and stop the current one. To do this you have to extract your media player logic from your page fragments.
try this code and see if it works
public class Diabetes1 extends Fragment {
MediaPlayer mp;
View rootView;
Boolean isPLaying = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.slidertopimagecardlayout, container, false);
SharedPreferences prefs = this.getActivity().getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
if (prefs.getString("myLanguage", "").equals("en")) {
mp = MediaPlayer.create(getActivity().getBaseContext(), getResources().getIdentifier("diab1en", "raw", this.getActivity().getPackageName()));
mp.start();
isPLaying = true;
} else if (prefs.getString("myLanguage", "").equals("es")) {
mp = MediaPlayer.create(getActivity().getBaseContext(), getResources().getIdentifier("diab1en", "raw", this.getActivity().getPackageName()));
mp.start();
isPLaying = true;
}
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
// If we are becoming invisible, then...
if (!isVisibleToUser) {
mp.stop();
} else {
// do what you like
}
}
}
#Override
public void onPause() {
super.onPause();
if(isPlaying) {
mp.stop();
isPLaying = false;
}
}
#Override
public void onResume() {
super.onResume();
}
}

android studio ethereum wallet loading message

I am doing an app that creates an ethereum wallet and send some ether when you touch the button register, it takes about 1 minute to do this, while it happens I want to show a message saying: Creating a wallet, wait please.
When I show the message it won't create the wallet or it will create the wallet but it won't show the message.
PS: If someone knows how to down the time to do this, it will help me a lot.
Thanks
thanks for the help... and the negative.
I have solved it anyway, I leave my solution below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ActivityMain);
errorMsg = new AlertDialog.Builder(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyAsyncTasks().execute();
}
});
}
class MyAsyncTasks extends AsyncTask<Void, Void, String> {
// ProgressDialog dialog;
#Override
//It will show a message during your background
protected void onPreExecute() {
dialog = new ProgressDialog(mainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Add title");
dialog.setMessage("Add message");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected void onPostExecute() {
dialog.dismiss(); // Close the Dialog
//Show a window with error or operation succesfully
if(hash.equals("Error")){
displayError("Error", "Error");
dialog.cancel();
}
else{
displayConfirmation("Operation Succesfully");
dialog.cancel();
}
}
#Override
protected String doInBackground(Void... voids) {
dialog.show();
// your code to do you background activity
}
}

If Statement Not Executed in AutocompleteTextView

I'm new to Android Studio and currently working on my first app. Thanks to numerous examples on here i have managed to catch up on the basics very quickly. I now have a problem with my autocompletetextview, i want to get the text selected then use it in a if statement to determine what is shown on the "results" textview. The if statement works fine but only for the "else" part and i can't figure out where i went wrong.
I have commented out the onItemClickListener because the "testfoodph" method does the same.
Here is the activity code:
public class phTester extends AppCompatActivity {
Intent intent = getIntent();
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
TextView results;
String foodies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ph_tester);
String[] foods = getResources().getStringArray(R.array.foodlist);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
text.setAdapter(adapter);
text.setThreshold(1);
results=(TextView)findViewById(R.id.phResults);
foodies = text.getText().toString();
text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//if (foodies.equalsIgnoreCase("Mango")) {
//results.setText(getString(R.string.phAlk));
}
//else {
//results.setText(getString(R.string.Error));
//}
//}
});
}
public void TestFoodPH(View view) {
if (foodies.equalsIgnoreCase("Mango")) {
results.setText(getString(R.string.phAlk));
}
else {
results.setText(getString(R.string.Error));
}
}

Resources