d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Looking For Android Help > Threading, And Qr Reader Help
Add Reply New Topic New Poll
Member
Posts: 8,154
Joined: Mar 28 2010
Gold: 23.70
Mar 26 2014 10:48pm
I'm looking for help with my Android App code. So far, I am using aSyncTask to run a simple JSON get request in the background, but for my needs, it needs to do this and update(i.e.- looper class in a thread, hopefully, if I understand what a looper thread is correctly?)


So my first question is - How would I go about adding a class that creates a looper thread, where I can run a JSON request, then update the text on the UI of my app?

followed by two secondary questions, that I have not done enough research to be at a loss for, but if anyone in here is fairly skilled and can explain it better than the few notes I've read on it, that would appreciated too!

1.1) How do QR readers work, and what would be a simple way of implementing one into an app?
1.2) How does saving data/caching multiple strings work in Android? I understand the sharepreferences concept, but I got an error on load, so I then just removed it as that is the least important concept of my project, for the time being.

Like I said, in importance, it basically goes from top to bottom, and I'd really appreciate any and all help. This is my first post in this forum, so let me know if I need to explain anything more/provide an idea of what sort of code I'm trying to do.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 27 2014 06:25am
Quote (Eldunari @ Mar 26 2014 11:48pm)
I'm looking for help with my Android App code. So far, I am using aSyncTask to run a simple JSON get request in the background, but for my needs, it needs to do this and update(i.e.- looper class in a thread, hopefully, if I understand what a looper thread is correctly?)


So my first question is - How would I go about adding a class that creates a looper thread, where I can run a JSON request, then update the text on the UI of my app?

i'm not clear what a "looper thread" is, but this is what you want to asynchronously update the ui:
http://developer.android.com/reference/android/os/AsyncTask.html

Quote

1.1) How do QR readers work, and what would be a simple way of implementing one into an app?

unless you have a super strong reason against this, i suggest firing an intent to open another app on the phone (can be set up to prompt user to install), the other app scans the qr code, then returns the result to your app. i used zxing, but i'm sure there are others.

Quote
1.2) How does saving data/caching multiple strings work in Android? I understand the sharepreferences concept, but I got an error on load, so I then just removed it as that is the least important concept of my project, for the time being.

Why don't you explain what you're trying to do? "saving/caching" doesn't tell me much. are you trying to persist data across activities? why aren't you using a database? do you have large data and you're trying to read only a small part at a time?
http://developer.android.com/guide/topics/data/data-storage.html#pref
Member
Posts: 8,154
Joined: Mar 28 2010
Gold: 23.70
Mar 27 2014 07:37am
Quote (carteblanche @ Mar 27 2014 07:25am)
i'm not clear what a "looper thread" is, but this is what you want to asynchronously update the ui:
http://developer.android.com/reference/android/os/AsyncTask.html


unless you have a super strong reason against this, i suggest firing an intent to open another app on the phone (can be set up to prompt user to install), the other app scans the qr code, then returns the result to your app. i used zxing, but i'm sure there are others.


Why don't you explain what you're trying to do? "saving/caching" doesn't tell me much. are you trying to persist data across activities? why aren't you using a database? do you have large data and you're trying to read only a small part at a time?
http://developer.android.com/guide/topics/data/data-storage.html#pref


I've already setup an asynctask, so if that's the best way to go about what I am doing, how would I go about forcing text changes to a text view, or something along those lines?

I have no reason against anything, I just want to adapt a qr reader to fit. I looked into zxing, but I wasn't entirely clear on how to implement that code into an existing project

As far as data storage goes, the qr reader should be pulling a string, and I'd like to save what it pulls to the data storage. Like I said, this is the topic I haven't delved too much into, so if I find a decent tutorial I should be able to get it
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 27 2014 09:15am
I'd use a timer for sending async requests.

Code
Timer myTimer = new Timer( );
myTimer.schedule( new TimerTask( ) {
@Override
public void run( ) {
new MyAsyncTask( ).execute( );
}
}, 0, TIME_BETWEEN_UPDATES );



1.1 For using zxing, install it on your phone (download from the google play store), then in your app, you can call
Code
startActivityForResult( new Intent(
"com.google.zxing.client.android.SCAN" ), 0 );

Also override the function OnActivityResult
Code
@Override
public void onActivityResult( int requestCode, int resultCode, Intent intent ) {

if ( resultCode == RESULT_OK ) {
String scanResult = intent.getStringExtra( "SCAN_RESULT" );
}

}


This post was edited by labatymo on Mar 27 2014 09:16am
Member
Posts: 8,154
Joined: Mar 28 2010
Gold: 23.70
Mar 27 2014 12:55pm
Quote (labatymo @ Mar 27 2014 10:15am)
I'd use a timer for sending async requests.

Code
Timer myTimer = new Timer( );
    myTimer.schedule( new TimerTask( ) {
      @Override
      public void run( ) {
        new MyAsyncTask( ).execute( );
      }
    }, 0, TIME_BETWEEN_UPDATES );



1.1 For using zxing, install it on your phone (download from the google play store), then in your app, you can call
Code
startActivityForResult( new Intent(
                        "com.google.zxing.client.android.SCAN" ), 0 );

Also override the function OnActivityResult
Code
@Override
  public void onActivityResult( int requestCode, int resultCode, Intent intent ) {

    if ( resultCode == RESULT_OK ) {
      String scanResult = intent.getStringExtra( "SCAN_RESULT" );
    }

  }


so, my async works well, but I'm having issues setting up the timer. Below I'll post the code I'm using to loop:

Code

public void onToggle(View view) throws JSONException, IOException {

((RadioGroup)view.getParent()).check(view.getId());
final TextView poolInfoToChange = (TextView) findViewById(R.id.editablePoolInfotextView);
final TextView userInfoToChange = (TextView) findViewById(R.id.editableUserInfotextView);



t.schedule(new TimerTask() {

@Override
public void run() {
//TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
poolInfoToChange.setText(
poolName+"\n"+workerCount+"\n"+currentDiff);
userInfoToChange.setText(
username+"\n"+totalHashrate+"\n"+roundShares+"\n"+roundEstimate);
}
});
}
}, 0, 60000);

Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 27 2014 01:33pm
Looks good to me. Are you getting any errors?
Member
Posts: 8,154
Joined: Mar 28 2010
Gold: 23.70
Mar 27 2014 01:38pm
Quote (labatymo @ Mar 27 2014 02:33pm)
Looks good to me. Are you getting any errors?


Oh wait, actually just got it to work, thank you so much!

This post was edited by Eldunari on Mar 27 2014 01:42pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 27 2014 02:07pm
Quote (Eldunari @ Mar 27 2014 03:38pm)
Oh wait, actually just got it to work, thank you so much!


np
Member
Posts: 8,154
Joined: Mar 28 2010
Gold: 23.70
Mar 27 2014 02:27pm
Quote (labatymo @ Mar 27 2014 03:07pm)
np


Okay, Now I have a different issue(I don't have any more errors, but this isn't performing what I want it to do):

When I click from toggle to toggle, it's I guess getting backed up with data in the background, because it's not pulling data from the JSON. Below is the entire code that I've wrote for my onToggle() method.


Code

public void onToggle(View view) throws JSONException, IOException {

final TextView poolInfoToChange = (TextView) findViewById(R.id.editablePoolInfotextView);
final TextView userInfoToChange = (TextView) findViewById(R.id.editableUserInfotextView);
((RadioGroup)view.getParent()).check(view.getId());
if(view.getId() == R.id.LTCtoggleButton) {
new DownloadPoolInfo().execute("https://www.wemineltc.com/api");
new DownloadUserInfoLTC().execute("https://www.wemineltc.com/api?api_key="+LTCapi);
LTCactive = true;
FTCactive = ALLactive = false;
}else if(view.getId() == R.id.FTCtoggleButton) {
new DownloadPoolInfo().execute("http://www.wemineftc.com/api");
new DownloadUserInfoFTC().execute("http://www.wemineftc.com/api?api_key="+FTCapi);
FTCactive = true;
LTCactive = ALLactive = false;
}else if(view.getId() == R.id.ALLtoggleButton) {
new DownloadPoolInfo().execute("https://www.wemineall.com/api");
new DownloadUserInfoLTC().execute("https://www.wemineall.com/api?api_key="+ALLapi);
ALLactive = true;
LTCactive = FTCactive = false;
}
t.schedule(new TimerTask() {
@Override
public void run() {
//TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {

if(LTCactive) {
poolInfoToChange.setText(
LTCpoolName+"\n"+LTCworkerCount+"\n"+LTCcurrentDiff);
userInfoToChange.setText(
LTCusername+"\n"+LTCtotalHashrate+"\n"+LTCroundShares+"\n"+LTCroundEstimate);
}else if(FTCactive) {
poolInfoToChange.setText(
FTCpoolName+"\n"+FTCworkerCount+"\n"+FTCcurrentDiff);
userInfoToChange.setText(
FTCusername+"\n"+FTCtotalHashrate+"\n"+FTCroundShares+"\n"+FTCroundEstimate);
}else if(ALLactive) {
poolInfoToChange.setText(
ALLpoolName+"\n"+ALLworkerCount+"\n"+ALLcurrentDiff);
userInfoToChange.setText(
ALLusername+"\n"+ALLtotalHashrate+"\n"+ALLroundShares+"\n"+ALLroundEstimate);

}
}
});
}
}, 0, 1000);
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll