TextView's Text is not updated in UI Thread - java

I've got a problem. I want to change the text of a textview after selecting an item (ringtone). I got the selected ringtone title and wanted to write it on the text attribute of textview, but it doesn't work (I am in the UI Thread not in a seperate thread).
Here's the code, where i want to update the text:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case RESULT_OK:
String ringtoneTitle = null;
ringtoneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if(ringtoneUri != null) {
ringtoneValue = ringtoneUri.toString(); //diesen Wert später in Datenbank abspeichern !!
ringtoneTitle = RingtoneManager.getRingtone(this, ringtoneUri).getTitle(this); //Titel getten
}
textViewRingtone.append(": " + ringtoneTitle); //Titel zu TextView setzen
break;
}
}
Can somebody help ?

Use the post(Runnable) method of the View class :
textViewRingtone.post(new Runnable() {
#Override
public void run() {
textViewRingtone.append(": " + ringtoneTitle);
}
};

Try setting textview inside a handler:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case RESULT_OK:
String ringtoneTitle = null;
ringtoneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if(ringtoneUri != null) {
ringtoneValue = ringtoneUri.toString(); //diesen Wert später in Datenbank abspeichern !!
ringtoneTitle = RingtoneManager.getRingtone(this, ringtoneUri).getTitle(this); //Titel getten
}
new Handler().post(new Runnable() {
#Override
public void run() {
textViewRingtone.append(": " + ringtoneTitle); //Titel zu TextView setzen
}
});
break;
}
}

OK,
i've found the solution, it was a really, really silly mistake of mine, that you couldnt know, because i didnt show that code to you.
In the onClick Method, in which i start the intent of showing the ringtone picker i accidentally called startActivity(intent) instead of startActivityForResult(intent, requestCode) so onActivityResult couldn't get called.
Thanks to #codeMagic, who made me realize this mistake.

Related

Integrating ZXing in my application

I need to build an app which will recognize QR codes without forcing the user to install other apps. In future, I will also need to manipulate the scanned image before recognizing the code (codes scanned by me will have inverted colors).
Trying to follow hints and tutorials from these links:
Integrating the ZXing library directly into my Android application
Embedding ZXing in android app
http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/
After creating some basic code and running the app I click my Scan button and get an error that No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }
What I've done:
Create new project
Copy core-3.2.1.jar to libs/
Add intent calling and result handling
intent/result code added by me:
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
MultiFormatWriter writer = new MultiFormatWriter();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
How to start the intent? What am I doing wrong?
you should launch scan like this:
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//in case you want to customize a bit.
integrator.setPrompt("Scan a QR/Bar code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
Receive results like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String data = scanResult.getContents();
// use this data
} else {
// error
}
break;
}
}
}
Edit 1:
Add this to build.gradle of your app as dependencies:
compile 'com.journeyapps:zxing-android-embedded:3.0.2#aar'
compile 'com.google.zxing:core:3.2.0'
Try the below link it worked for me:
https://github.com/dlazaro66/QRCodeReaderView

Calling onActivityResult to determine when a file has been received

I have a WebActivity that sets a JavascriptInterface for a WebView, on the Interface I have created various methods to be called with Javascript for different tasks, one of them is to send a file through Bluetooth.
The file sends just fine, but I want to inform the user when the file has already been accepted and transmited completely on the other end.
I am trying to use onActivityResult on my main WebActivity and also have tried on the separate class that handles the Bluetooth transmission and has a method called on the JavascriptInterface but I am not getting any callback results after the file is succesfully sent:
//First attempt
public class WebActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//some code here
//Here I set the interface
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == 0) {
//This message is shown after the file transfer has started but no
//finished
Toast.makeText(this, "File Sent", Toast.LENGTH_SHORT).show();
if(resultCode == Activity.RESULT_OK){
//It doesn't work here either I always get Activity.RESULT_CANCELED (0)
//flag afer the file transfer has started
}
}
}
}
//Second Attempt
public class BluetoothTasks {
//code here
private void startTransferIntent() {
//some more code here
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File file_to_transfer = new File(Environment.getExternalStorageDirectory() +
"/txtTransfer.txt");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file_to_transfer));
//some more code here
Activity act = (Activity) mContext;
act.startActivityForResult(intent, TRANSFER_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == TRANSFER_RESULT) {
//Not working here either
//code that is not executed
}
}
}
Could you please tell me if there is a way to do this and if so how would be accomplished?. Thank you.

Android Speech Recogntion won't work in a separate class

I'm a beginner in Android development. What I'm trying to create is an app the speaks to you then waits for your answer so its kind of text to speech works ,then it activates speech recognition and with my code the text to speech works then it calls speech recognition.Then an erro msg is shown ,the catch block is executed.
The problem is that I have to add Speech Recognition in a separate class, then add an Object of it in the Adapter not Main Activity. Speech Recognition won't work and since all tutorials for speech recognition for Android are adding the code in Main Activity and I'm doing it as class called in the adapter is the problem ?
UPDATE startActivityForResult() method is not working
Here is the code for SpeechRecogntion class
public class SpeechRecog extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
String textResult ;
public void Start (Intent i , Context c){
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are You Done , yet? ");
try{
startActivityForResult(i,VOICE_RECOGNITION_REQUEST_CODE);
} catch(Exception e) {
Toast.makeText(c, "SpeechRecogntion is not avalible on your device ", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == VOICE_RECOGNITION_REQUEST_CODE)&& (resultCode== RESULT_OK)) {
textResult= data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
if (checkResult())
Toast.makeText(getApplicationContext(), "You are done ", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "You are not done ", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private boolean checkResult() {
if (textResult.equals("done")||(textResult.equals("yes")))
return true ;
return false;
}
}
And Here is the object of it in the Adapter class
private SpeechRecog mySP;
private Intent mySpIntetn ;
public ListAdapterClass(Context context) {
mytts = new TxtToSpeechClass(context);
mySP = new SpeechRecog();
}
And Here is where my problem relies ,the on clicklistener inside the Adapter class
Lh.btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if (Lh.btn.getText().equals("Play"))
{
Lh.btn.setText("Pause"); //change the btn status so user know how to stop it
List<Items> hResults=db.getAllItems(Lh.idList + 1);
for (int i = 0; i < hResults.size(); i++)
{
Items item = hResults.get(i);
String s = item.getItemname();
mytts.Talk(s,1);
mySP.Start(mySpIntetn , context ); //the code in here wont work is there something I'm missing here
Toast.makeText(context,s, Toast.LENGTH_SHORT).show();
}
mytts.Talk("Congrats ",1);// 1 means Queue_add
} else {
Lh.btn.setText("Play");
mytts.Talk("See you later", 0); // 0 means Queue_Flush
}
}
);
mySpIntetn is private. Try to change it to public in the Adapter class.

Android Facebook-Connect button needs to be clicked two times

since hours I am trying to figure out why my facebook-connect is not working properly. I always have to click it two times.. the first time I click it I see that the facbeook dialog is opening but its not calling any kind of callback so my Login-Activity is just "resuming".
public void onClick_fbIcon(View v) {
if(!buttonsEnabled)
return;
buttonsEnabled = false;
tryFacebookConnect();
}
#SuppressWarnings("deprecation")
private void tryFacebookConnect() {
Log.d("debug", "start fbc");
doingFacebooklogin = true;
facebook.authorize(this, new DialogListener() {
public void onComplete(Bundle values) {
end();
toast("Complete");
Log.d("debug", "#0");
// .. continue with main code
}
public void onFacebookError(FacebookError error) {
end();
Log.d("debug", "#1");
}
public void onError(DialogError e) {
end();
Log.d("debug", "#2");
}
public void onCancel() {
end();
Log.d("debug", "#3");
}
private void end() {
Log.d("debug", "fbc complete");
doingFacebooklogin = false;
}
});
}
The first click I only get "start fbc" but nothing after that, so I guess that no callback method is called.
I found out that when the facebook-connect is not working the first time, "onResume is called". The next time onActivityResult is called.
#SuppressWarnings("deprecation")
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("debug", "Main: onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
My login activity is an intent started by my main activity that is specifing if a login is needed at all:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Sometimes I can see the following error:
11-22 10:23:39.684: D/Facebook-publish(7317): Can NOT get FbInjector instance! Probably because this method was called in ContentProvider's onCreate.
Please take a look at this Tutorial. I just Follow this and I make it all works perfectly.
Hope that it will help you too.
http://www.youtube.com/watch?v=xndi6s9Tn_U
I don't know why but the problem is obviously part of the facebook-sdk. I found out that this problem does not occur if I have no facebook-app installed on my device. So I changed my code like this:
facebook.authorize(a, new String[] { }, **Facebook.FORCE_DIALOG_AUTH**, new DialogListener() {
...

How to record video using Intents?

I need to unserstand how I can record video programatically. Now I use this construction:
public class AndroidLearningActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
When the application is opened then the camera will be opened too. I have record some video, but if I press "back" button on the device then the application crushes. Please, explain me, how can I do it? Thank you.
looks as you are pressing back key and data (intent) not get set.so data may be null here
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
You have problem in this statement
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
When you will press back button recording will be cancelled and you will get data.getData as null since no recording is done.So change your code to following.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
Log.e("result", "result:" + resultCode);
}
super.onActivityResult(requestCode, resultCode, data);
}

Categories