I am trying to speak out large amount of text using Android Text To Speech. I using default Google speech engine. Below is my code.
public class Talk extends Activity implements TextToSpeech.OnInitListener {
private ImageView playBtn;
private EditText textField;
private TextToSpeech tts;
private boolean isSpeaking = false;
private String finalText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);
//Intialize the instance variables
playBtn = (ImageView)findViewById(R.id.playBtn);
textField = (EditText)findViewById(R.id.textField);
//Resister the listeners
playBtn.setOnClickListener(new PlayBtnAction());
//Other things
tts = new TextToSpeech(this,this);
//Get the web page text if called from Share-Via
if (Intent.ACTION_SEND.equals(getIntent().getAction()))
{
new GetWebText().execute("");
}
}
//This class will execute the text from web pages
private class GetWebText extends AsyncTask<String,Void,String>
{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
String websiteText = "";
try {
//Create a URL for the desired page
URL url = new URL(text);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
StringBuffer strBuffer = new StringBuffer("");
while ((str = in.readLine()) != null)
{
strBuffer.append(str+"\n"+"\n");
}
in.close();
String html = strBuffer.toString();
Document doc = Jsoup.parse(html);
websiteText = doc.body().text(); // "An example link"
//Toast.makeText(this, websiteText, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Log.e("web_error", "Error in getting web text",e);
}
return websiteText;
}
#Override
protected void onPostExecute(String result)
{
textField.setText(result);
}
}
}
//Class to speak the text
private class PlayBtnAction implements OnClickListener
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(!isSpeaking)
{
isSpeaking = true;
//speak(textField.getText().toString());
finalText = textField.getText().toString();
new SpeakTheText().execute(finalText);
isSpeaking = false;
}
else
{
isSpeaking = false;
tts.stop();
}
}
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status==TextToSpeech.SUCCESS)
{
int result = tts.setLanguage(Locale.UK);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText(this, "Language Not Supported", Toast.LENGTH_LONG).show();
}
}
}
//This class will speak the text
private class SpeakTheText extends AsyncTask<String,Void,String>
{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
tts.speak(params[0], TextToSpeech.QUEUE_FLUSH, null);
return null;
}
}
#Override
public void onDestroy()
{
if(tts!=null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
But the issue here is, when there is a large chunk of text (lets say you have extracted text from a web page) the TTS fails to read it. If I remove most of the text, then it will read it. Why is this happening?
When I am about to read the large text, the LogCat however display something like this
10-11 07:26:05.566: D/dalvikvm(2638): GC_CONCURRENT freed 362K, 44% free 3597K/6312K, paused 17ms+8ms, total 93ms
The String length should not be longer than pre-defined length, from docs:
Parameters
text The string of text to be spoken. No longer than getMaxSpeechInputLength() characters.
Returned value by getMaxSpeechInputLength() may vary from device to device, but according to AOSP source that is whopping 4000:
/**
* Limit of length of input string passed to speak and synthesizeToFile.
*
* #see #speak
* #see #synthesizeToFile
*/
public static int getMaxSpeechInputLength() {
return 4000;
}
Try not to exceed that limit: compare input text length with that value and split into separate parts if necessary.
Use this code...Working for any file ..
just send the string to speech function..
private void speech(String charSequence) {
int position ;
int sizeOfChar= charSequence.length();
String testStri= charSequence.substring(position,sizeOfChar);
int next = 20;
int pos =0;
while(true) {
String temp="";
Log.e("in loop", "" + pos);
try {
temp = testStri.substring(pos, next);
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, temp);
engine.speak(temp, TextToSpeech.QUEUE_ADD, params);
pos = pos + 20;
next = next + 20;
} catch (Exception e) {
temp = testStri.substring(pos, testStri.length());
engine.speak(temp, TextToSpeech.QUEUE_ADD, null);
break;
}
}
}
In case someone might find this helpful. When you split the large text into strings, do not set the length of each string to the exact value of getMaxSpeechInputLength(). Subtract the string length by 1. Otherwise, only the last chunk of string could be read by TTS.
int length = toSpeech.getMaxSpeechInputLength() - 1;
Iterable<String> chunks = Splitter.fixedLength(length).split(largeText);
Lists.newArrayList(chunks);
It is worse than the 4000 characters limit in practice on Android. There are some TTS engines that limit the input length a lot more. For example Nuance.tts and vocalizer.tts engines won't speak any string longer than about 512 characters (from my tests some time ago). Today I hit a limit of below 300 characters in es.codefactory.eloquencetts package, which simply crashes if the string I send to it is more than 256-300 characters. I divide the contents into sentences, and guard for sentences longer than the above limit, further sub-dividing them in my app code.
Greg
If you follow ozbek's advice you should be fine. I too have large text files that I want spoken. I simply used the streamreader method and everything works fine. heres' PART of my code. it's the part that you should use. My code does a bit more than you want but it works for me and may work for you.
Dim sReader As StreamReader = New StreamReader(Story_file)
Try
Do Until EndOfStream '= True
Dim line_to_speak As String = sReader.ReadLine
Dim vc = Mid(line_to_speak, 1, 1) <- you dont need this
Select Case vc <- you dont need this
Case Is = "/" <- you dont need this
voice_index = Val(Mid(line_to_speak, 2, 2)) <- you dont need this
srate = Val(Mid(line_to_speak, 5, 2)) <- you dont need this
edassistv.lstVoices.SelectedIndex = voice_index <- you dont need this
selected_voice = edassistv.lstVoices.SelectedItem <- you dont need this
Case Else<- you dont need this
synth.SelectVoice(selected_voice)
synth.Speak(line_to_speak)
End Select<- you dont need this
Loop
Catch ex As Exception
GoTo finish
Related
So I want to dynamically add buttons to my layout in android studio, but I don't know how many buttons I need in the "onCreate" method. So I just declare and define 50 buttons that I later add into the layout.
public class ChooseMatchupActivity extends AppCompatActivity {
//THIS IS PROBLEM #1 RIGHT HERE!!! I DON'T KNOW HOW MANY BUTTONS I CAN HAVE UNTIL AFTER
// I GET THE NUMBER OF MATCHUPS FOR THE DAY FROM THE API, SO I JUST DECLARE 50
Button matchupButtons[] = new Button[50];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_matchup);
String apiFeedUrl = "https://api.somesportssite.com/pull/current/daily_game_schedule.json?fordate=20180113";
ll = findViewById(R.id.matchup_layout);
lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//THIS IS PROBLEM #2 RIGHT HERE!!! I DON'T KNOW HOW TO DEFINE ALL OF THE BUTTONS IN THE "RetrieveFeedTask" CLASS
for(int i = 0; i < 50; i++) {
matchupButtons[i] = new Button(this);
matchupButtons[i].setTextSize(30);
matchupButtons[i].setGravity(Gravity.START);
}
// Get data feed from API
new RetrieveFeedTask().execute(apiFeedUrl);
}
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {}
protected String doInBackground(String... urls) {
String url = urls[0];
String response = null;
URLConnection connection = new URL(url).openConnection();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader in = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
response = stringBuilder.toString();
return response;
}
protected void onPostExecute(String response) {
/* Grab all matchups from API and add them to the layout */
try{
JSONObject obj = new JSONObject(response);
JSONArray games = obj.getJSONObject("dailygameschedule").getJSONArray("gameentry");
//WOULD LIKE TO DECLARE AND DEFINE ALL BUTTONS RIGHT HERE
for (int i = 0; i < games.length(); i++){
String awayTeam = games.getJSONObject(i).getJSONObject("awayTeam").getString("Name");
String homeTeam = games.getJSONObject(i).getJSONObject("homeTeam").getString("Name");
String matchUpStr = awayTeam + "\n" + homeTeam;
matchupButtons[i].setText(matchUpStr);
ll.addView(matchupButtons[i], lp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
I made comments where I felt the most work was needed. I would like to declare and define all of the buttons in the "onPostExecute" method. Is there a way to do this? I'm starting to think it might be good to make a call to the api first in the "onCreate" method to find out what the size needs to be. Thanks.
If you don't know what size you need your array to be you should use an ArrayList, it can expand to the required size.
As #Ivan Wooll said, since I didn't know what size I needed the array of buttons to be, I should use an ArrayList. I am just going to post the code in case anyone wants to see the solution:
instead of:
Button matchupButtons[] = new Button[50];
I did:
ArrayList<Button> matchupButtons = new ArrayList<>();
I took out the for loop in the onCreate method that defined all of the buttons and put it in the onPostExecute method in the RetrieveFeedTask class.
protected void onPostExecute(String response) {
Log.v(TAG, "HTTP Response: " + response);
/* Grab all matchups from API and add them to the layout */
try{
JSONObject obj = new JSONObject(response);
JSONArray games = obj.getJSONObject("dailygameschedule").getJSONArray("gameentry");
for (int i = 0; i < games.length(); i++){
Button button = new Button(getApplicationContext());
button.setTextSize(30);
button.setGravity(Gravity.START);
matchupButtons.add(button);
String awayTeam = games.getJSONObject(i).getJSONObject("awayTeam").getString("Name");
String homeTeam = games.getJSONObject(i).getJSONObject("homeTeam").getString("Name");
String matchUpStr = awayTeam + "\n" + homeTeam;
matchupButtons.get(i).setText(matchUpStr);
ll.addView( matchupButtons.get(i), lp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I would now like to set the text size and gravity for all of the buttons in the ArrayList instead of having to set it every time a new button is created in the for loop as shown above. Let me know if there is a way to set that. Thanks!
So I'm trying to make my first ever Android app. It's just a simple tally counter for now but I've come across a weird bug.
You see, I've got a simple save and load function, an increment, decrement and reset button. and a TextView that displays the value all together (see below).
Now when I increment the value up to say 10 and close the app, it saves as it should and when I open the app again, it does come back as 10. However, when I then increment up to say 30 or 100, close and restart the value does not stick and comes out as either -1 or a value that is completely different.
What could be happening?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
int value;
TextView textView_value;
final String filename = "tallyCountPlus";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadValue(filename);
textView_value = (TextView) findviewById(R.id.textView_value);
textView_value.setText(Integer.toString(value));
}
/** Called when the user presses the increment button */
public void incValue(View view) {
value++;
textView_value.setText(Integer.toString(value));
}
/** Called when the user presses the decrement button */
public void decValue(View view) {
if (value > 0) {
value--;
}
textView_value.setText(Integer.toString(value));
}
/** Called when the user presses the reset button */
public void resetValue(View view) {
value = 0;
textView_value.setText(Integer.setString(value));
}
#Override
public void onPause() {
super.onPause();
saveValue(filename, Context.MODE_PRIVATE);
}
#Override
public void onResume() {
super.onResume();
loadValue(filename);
textView_value.setText(Integer.toString(value));
}
public void onDestroy() {
super.onDestroy();
saveValue(filename, Context.MODE_PRIVATE);
}
private void loadValue(String name) {
try {
FileInputStream fis = openFileInput(name);
value = fis.read();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveValue(String name, int context) {
try {
FileOutputStream fos = openFileOutput(name, context);
fos.write(value);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Anyone know what my problem could be?
Thanks.
Jamie.
EDIT:
So I think I've fixed the bug by doing:
private void loadValue(String filename) {
/* this is wrapped in a try-catch statement */
FileInputStream fis = openFileInput(name);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
value = Integer.parseInt(sb.toString());
fis.close() /* Not sure if this is actually neccessary? */
if (value == -1) {
value = 0;
}
}
private void saveValue(String name) {
/* This is also wrapped in a try-catch statement */
FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE);
String data = Integer.toString(value);
fos.write(data.getBytes());
fos.close();
}
And after putting in a couple of values, it seems to work fine. However, I'm unsure as to whether it is appending or deleting the previous value and replacing it... I'd rather the latter to prevent the save file from getting too big.
onDestroy() is not dependable. Just stick with onPause(). Also, in general, for the methods that handle events when the activity is "going away", you should call the super method after your own code, to ensure your custom code can still execute.
Other things to check might be:
Is the method that saves the output replacing the previous file, or just appending to it?
Are you properly loading and saving each time?
If the file is properly loaded, is the value making it into the TextView? You might need to construct an anonymous Runnable that ensures that the TextView is updated by the main (UI) thread.
Sprinkle log statements liberally throughout the code to keep track of what the tally is, what you get out of the file, etc.
When diagnosing file issues, it helps to test on an emulator, which gives you root access to the device storage.
You need to modify loadValue() method. You are assigning fis.read() to value which returns next byte or -1 if EOF reached.
private void loadValue(String name) {
try {
FileInputStream in = openFileInput(name);
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
value = Integer.parseInt(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
So Im having trouble using Microsoft's Emotion API for Android. I have no issues with regards to running the Face API; Im able to get the face rectangles but I am not able to get it working on the emotion api. I am taking images using the builtin Android camera itself. Here is the code I am using:
private void detectAndFrame(final Bitmap imageBitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
ByteArrayInputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray());
AsyncTask<InputStream, String, List<RecognizeResult>> detectTask =
new AsyncTask<InputStream, String, List<RecognizeResult>>() {
#Override
protected List<RecognizeResult> doInBackground(InputStream... params) {
try {
Log.e("i","Detecting...");
faces = faceServiceClient.detect(
params[0],
true, // returnFaceId
false, // returnFaceLandmarks
null // returnFaceAttributes: a string like "age, gender"
);
if (faces == null)
{
Log.e("i","Detection Finished. Nothing detected");
return null;
}
Log.e("i",
String.format("Detection Finished. %d face(s) detected",
faces.length));
ImageView imageView = (ImageView)findViewById(R.id.imageView);
InputStream stream = params[0];
com.microsoft.projectoxford.emotion.contract.FaceRectangle[] rects = new com.microsoft.projectoxford.emotion.contract.FaceRectangle[faces.length];
for (int i = 0; i < faces.length; i++) {
com.microsoft.projectoxford.face.contract.FaceRectangle rect = faces[i].faceRectangle;
rects[i] = new com.microsoft.projectoxford.emotion.contract.FaceRectangle(rect.left, rect.top, rect.width, rect.height);
}
List<RecognizeResult> result;
result = client.recognizeImage(stream, rects);
return result;
} catch (Exception e) {
Log.e("e", e.getMessage());
Log.e("e", "Detection failed");
return null;
}
}
#Override
protected void onPreExecute() {
//TODO: show progress dialog
}
#Override
protected void onProgressUpdate(String... progress) {
//TODO: update progress
}
#Override
protected void onPostExecute(List<RecognizeResult> result) {
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(drawFaceRectanglesOnBitmap(imageBitmap, faces));
MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "AnImage" ,"Another image");
if (result == null) return;
for (RecognizeResult res: result) {
Scores scores = res.scores;
Log.e("Anger: ", ((Double)scores.anger).toString());
Log.e("Neutral: ", ((Double)scores.neutral).toString());
Log.e("Happy: ", ((Double)scores.happiness).toString());
}
}
};
detectTask.execute(inputStream);
}
I keep getting the error Post Request 400, indicating some sort of issue with the JSON or the face rectangles. But I'm not sure where to start debugging this issue.
You're using the stream twice, so the second time around you're already at the end of the stream. So either you can reset the stream, or, simply call the emotion API without rectangles (ie skip the call to the face API.) The emotion API will determine the face rectangles for you.
I'm trying to figure out exactly how to implement Google Play Game services in my app. On the Google Play Developer Console I've linked the game the relevant app using the SHA1 key and I know how to add leaderboards and achievements on here. I have also installed the Google Play services and Google Repository to Android Studio and added the dependency into build.gradle (as explained here: http://developer.android.com/google/play-services/setup.html) but I wasn't sure how to do the last 2 steps on that page (creating a Proguard exception and ensuring devices have the Google Play services APK) and if they are necessary - the latter of which even the Google Play Games sample projects don't seem to do.
Moreover, I'm not sure what code I actually need to put in my project to enable leaderboards and achievements, since according to this guide: https://developers.google.com/games/services/android/achievements, I use this code:
Games.Achievements.unlock(mGoogleApiClient, "my_achievement_id");
to unlock an achievement, for example, but there are no instructions on how I set up mGoogleApiClient. I've looked at the sample projects but it's still not clear what I'm supposed to do. Am I meant to copy and paste all of the code into my project? Are there certain sections I'm supposed to copy and paste? Do I have to write my own code for signing in to Google Play Games?
You should use getApiClient() instead of mGoogleApiClient.
Assuming you have an activity with a layout containing four buttons:
Leaderboard button - To launch the leaderboards,
Achievement button - To launch achievements
Sign in and Sign out buttons
For leaderboard, we push scores for best minutes, best distance covered and an overall highscore.
And for achievements, we unlock four achievements - survivor, warrior, lord, pride based on certain conditions.
Here's how you'd go about it:
public class GMSActivity extends BaseGameActivity implements OnClickListener{
Button lead;
Button achv;
final int RC_RESOLVE = 5000, RC_UNUSED = 5001;
//your game score. so we can push to cloud
int hS = 0;
//flags for achievements
boolean survivor;
boolean tribalWarriror;
boolean akonfemLord;
boolean zuluPride;
LinearLayout signInBar;
LinearLayout signOutBar;
Resources r;
private float bestTimeInSeconds;
private int bestTimeInMinutes;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.gms_layout);
r = getResources();
lead = (Button)findViewById(R.id.leaderboards);
achv = (Button)findViewById(R.id.achievements);
hS = loadScores();
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
lead.setOnClickListener(this);
achv.setOnClickListener(this);
signInBar = (LinearLayout)findViewById(R.id.sign_in_bar);
signOutBar = (LinearLayout)findViewById(R.id.sign_out_bar);
checkForAchievements();
if (isSignedIn()) {
onSignInSucceeded();
}else{
onSignInFailed();
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == lead){
if (isSignedIn()) {
startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(getApiClient()), RC_UNUSED);
} else {
showAlert(getString(R.string.leaderboards_not_available));
}
}else if(v ==achv){
if (isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), RC_UNUSED);
} else {
showAlert(getString(R.string.achievements_not_available));
}
}else if(v.getId() == R.id.sign_in_button){
beginUserInitiatedSignIn();
}else if(v.getId() == R.id.sign_out_button){
signOut();
hello.setText(getString(R.string.signed_out_greeting));
signInBar.setVisibility(View.VISIBLE);
signOutBar.setVisibility(View.GONE);
}
#Override
public void onSignInFailed() {
hello.setText(getString(R.string.signed_out_greeting));
signInBar.setVisibility(View.VISIBLE);
signOutBar.setVisibility(View.GONE);
}
#Override
public void onSignInSucceeded() {
signInBar.setVisibility(View.GONE);
signOutBar.setVisibility(View.VISIBLE);
// Set the greeting appropriately on main menu
Player p = Games.Players.getCurrentPlayer(getApiClient());
String displayName;
if (p == null) {
// Log.w(TAG, "mGamesClient.getCurrentPlayer() is NULL!");
displayName = "";
} else {
displayName = p.getDisplayName();
}
// hello.setText("Hello, " + displayName);
pushAccomplishments();
Toast.makeText(this, getString(R.string.your_progress_will_be_uploaded),
Toast.LENGTH_LONG).show();
}
//check for achievements and unlock the appropriate ones
void checkForAchievements() {
// Check if each condition is met; if so, unlock the corresponding
// achievement.
bestTimeInSeconds = loadGameBestTimeSec();
if(bestTimeInSeconds >= 900){ //15 minutes
survivor = true;
tribalWarriror = true;
}
}
void pushAccomplishments() {
if (survivor)
Games.Achievements.unlock(getApiClient(), getString(R.string.achievement_survivor));
if (tribalWarriror)
Games.Achievements.unlock(getApiClient(), getString(R.string.achievement_tribal_warrior));
if(bestTimeInSeconds >= 60){ //1 minute atleast
bestTimeInMinutes = (int)bestTimeInSeconds/60;
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_best_time_minutes), bestTimeInMinutes);
}
if(bestTimeInSeconds >= 10){ // 1 meter atleast
int bestDistance = (int)bestTimeInSeconds/10;
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_best_distance_meters), bestDistance);
}
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_top_score_points), hS);
}
#Override
//loading scores and achievements
private int loadScores() {
// TODO Auto-generated method stub
int score = 0;
try{
preferences = new SecurePreferences(getApplicationContext(),
"besiPreferences", "1234", true);
score = Integer.parseInt(preferences.getString("highScore"));
}catch(Exception e){}
return score;
}
private float loadGameBestTimeSec() {
float time = 0;
try{
preferences = new SecurePreferences(getApplicationContext(),
"besiPreferences", "1234", true);
time = Float.parseFloat(preferences.getString("gameTimeSec"));
}catch(Exception e){}
return time;
}
private int loadCalabashesCompleted() {
try{
preferences = new SecurePreferences(getApplicationContext(),
"makolaPref", "1234", true);
return Integer.parseInt(preferences.getString("bookCompleted")== null ? "0" : preferences.getString("bookCompleted"));
}catch(Exception e){
return 0;
}
}
private int loadLevelCompleted() {
try{
preferences = new SecurePreferences(getApplicationContext(),
"makolaPref", "1234", true);
return Integer.parseInt(preferences.getString("levelCompleted")== null ? "0" : preferences.getString("levelCompleted"));
}catch(Exception e){
return 0;
}
}
}
What I want to do: get the id of the src of an ImageView, compare it to the ids of two drawables, and swap them, using AsyncTask (just because I want to understand how it works).
I've read similar questions here, and so far this is what I've got:
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.img);
Integer integer = (Integer) image.getTag();
}
private class cambiarImagen extends AsyncTask<Integer, Integer, Integer> {
protected void onPreExecute() {
ImageView image = (ImageView) findViewById(R.id.img);
Integer integer = (Integer) image.getTag();
int img1 = R.drawable.zapato;
int img2 = R.drawable.zapatod;
}
#Override
protected Integer doInBackground(Integer... values) {
// parte logica
int num = values[0];
int zapato = values[1];
int zapatod = values[2];
if (num == zapato) {
num = zapatod;
} else if (num == zapatod) {
num = zapato;
}
return num;
}
protected Void onPostExecute(Integer... values) {
int num = values[0];
ImageView image = (ImageView) findViewById(R.id.img);
image.setTag(num);
return null;
}
}
Of course this doesn't work.
1. I don't understand how to get the id of the drawable that ImageView has as its src.
2. I don't understand how the params are passed in AsyncTask; onPreExecute should receive the UI stuff, doInbackground should receive it to compare it and return the drawable int that should be set to the ImageView, and onPreExecute should set it to the ImageView.
I don't understand how to get the id of the drawable that ImageView has as its src.
I haven't had to do this so may not work but you should be able to use
imageView.getDrawable().getId();
I don't understand how the params are passed in AsyncTask;
Whatever you pass in task.execute() is received by doInBackground(). If you call publishProgress() then whatever params are sent there are received by onProgressUpdate(). And the data returned in doInBackground() is received by onPostExecute().
AsyncTask, just so you know, shouldn't be needed for this but I know you said you wanted to learn how to use it. I was a little confused on exactly what specifically you were having trouble with besides these two things so please elaborate if I missed something.
ImageView Docs
AsyncTask Docs
AsyncTask Example (in case it can be helpful)
You should do other task if you like to get to learn ASyncTask.
I would have done a dialog with progress bar or something instead if i wanted to learn ASyncTask.
edit:
As Samus Arin comment on the main post about you should always have track on which image that you are showing. so instead use something like
if(currentImage == R.Drawable.image1){
image.setImageResource(R.Drawable.image2);
}else{
image.setImageResource(R.Drawable.image1);
}
For what it's worth, take a look at what I'm doing with an AsyncTask, maybe it'll give ya some ideas.
This is Monodroid/C# code, not raw Java/Android (but the syntax is extremely close). As such, inner-classes do not get an implicit reference to their containing object, and so I pass one in (called outer in the constructor). I chose to name it "_" as a lexicographical extension to .NET's _member naming convention for private data members.
public class MonthChangeTask : AsyncTask
{
private CalendarView _; // outer class
private DateTime _newMonth;
private bool _refreshInspectionRecordsRemote;
private bool _changingInspector;
private bool _todayButtonPressed;
private Android.App.ProgressDialog _progressDialog;
private IMXController _controller;
private Dictionary<string, string> _paramz;
private DateTime _newSelectedDate;
public MonthChangeTask( CalendarView outer, DateTime newMonth, bool changingInspector, bool refreshInspectionRecordsRemote, bool todayButtonPressed )
{
_ = outer;
_newMonth = newMonth;
_changingInspector = changingInspector;
_refreshInspectionRecordsRemote = refreshInspectionRecordsRemote;
_todayButtonPressed = todayButtonPressed;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
_progressDialog = Android.App.ProgressDialog.Show( _ , "", "Loading Inspections...");
_newSelectedDate = _._calendar.SetMonth(new DateTime(_newMonth.Year, _newMonth.Month, 1));
AppSettingService.SetCalendarDate(_newMonth);
_paramz = new Dictionary<string, string>();
string target = MD.MxNAVIGATION.CONTROLLER.CALENDAR._name;
string action = MD.MxNAVIGATION.CONTROLLER.ACTION.GET;
string command = _refreshInspectionRecordsRemote
? ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsRemote).ToString()
: ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsLocal).ToString();
string url = target + "/" + action + "/" + command;
_controller = MXContainer.Instance.GetController(url, ref _paramz);
}
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] #params)
{
if ( _paramz == null )
{
Log.Info(FIDB.TAG_APP, "MonthChangeTask.DoInBackground(): paramz = NULL");
}
else
{
_controller.Load( _paramz );
}
return true;
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
_progressDialog.Dismiss();
_.Model = (CalendarVM)_controller.GetModel();
if (_changingInspector)
{
_._calendar.PermitSwitch = _.Model.Buttons.PermitsVisible;
_._calendar.ComplaintSwitch = _.Model.Buttons.ComplaintsVisible;
_._calendar.ProjectSwitch = _.Model.Buttons.ProjectsVisible;
_._calendar.PeriodicInspectionSwitch = _.Model.Buttons.PeriodicInspectionsVisible;
}
_.UpdateCalendar(_.Model.Inspections);
if( _todayButtonPressed )
{
_._calendar.SelectedDate = _._calendar.CurrentDate;
}
else
{
_._calendar.SelectedDate = _newSelectedDate;
}
_._calendar.Invalidate();
AppSettingService.SetCalendarDate( _._calendar.SelectedDate );
if ( _.Model.IsParcelCacheDownloading )
{
AnimationTask task = new AnimationTask( _ );
task.Execute( new Java.Lang.Object[1] );
}
}
}