Hey I try to make bluetooth service according to the information on the official android dev website. But I came across to the deprecate method startActivityForResult(). What should I do to properly turn on bluetooth device?
Here is my code with deprecated method:
private void enableBt(View view){
if(myBluetoothAdapter==null){
//we don't have bt in this device
}
else if(!myBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
It’s a special AndroidX extension that wraps the startActivityForResult and provide sort of a simpler method.
According to Android Developers, you need to extend ActivityResultContract, in which you implement an input/output for the activity result call by overriding 2 methods:
Method for creating intent based on the input.
Method for parsing an output based on the activity result.
In your case you don’t have an input so you can use Void type for the input (don’t know about your output though).
After properly implementing that contract class, you simply create an instance from that class and pass it to registerForActivityResult(…) (before your activity is started), which returns some sort of a launcher.
You use that launcher and call launch instead of startActivityForResult.
Enjoy :)
Related
My Android app features a text input box that has a button on the right of the EditText to call the voice-input feature.
I am porting the app with Codename One. At present time the iOS port is the goal.
The button has a suitable icon. This is the code:
voiceInputButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
try {
activity.startActivityForResult(voiceIntent, RESULT_SPEECH_REQUEST_CODE);
} catch (ActivityNotFoundException ex) {
}
}
});
It works very well, the voice-input screen is called and then the result is passed back to the app as a string.
The string is what the user said (for example, a single word).
I need to have this functionality in the CodenameOne app for iOS.
What should be the equivalent? Is it necessary to call native iOS functions, through the native interface?
You can implement speech-to-text via Speech framework, to perform speech recognition on live or prerecorded audio. More info: https://developer.apple.com/documentation/speech
About Codename One, you can create a native interface using Objective-C code.
To use the Speech framework with Objective-C, see this answer:
https://stackoverflow.com/a/43834120
The answer says so: «[...] To get this running and test it you just need a very basic UI, just create an UIButton and assign the microPhoneTapped action to it, when pressed the app should start listening and logging everything that it hears through the microphone to the console (in the sample code NSLog is the only thing receiving the text). It should stop the recording when pressed again. [...]». This seems very close to what you asked.
Obviously the creation of the native interface takes time. For further help, you can ask more specific questions, I hope I have given you a useful indication.
Lastly, there are also alternative solutions, again in Objective-C, such as: https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/objectivec/ios/from-microphone
You can search on the web for: objective-c speech-to-text
I'm trying to understand Android Intents and I understand the concept of a Java event and an EventListener. Are these similar or the the same?
Intent in Android, is used to express an "intention" as the name implies, to perform an action over another component. One common usage of intent is to use it along with startActivity(intentObject), where the object contains the information(contexts) of the starting and openning activity, see below:
Intent intent = new Intent(context, MyActivityB);//pass the information(contexts of the current activity and the activity I want to open).
startActivity(intent);//Use startActivity method to start the activity defined in the object intent.
Other actions include: openning an app to send an email, open a social media, etc.
I need to pass data from Activity A to Activity B and backwards in background, because Activity A has method, which initializing class, which is managing bluetooth connection, and I cant initialize that class from Activity B, because method in Activity A has already initialized it.
I tried to make method that I need to make static, but from static method I cant call non-static methods from my bluetooth class:
public static void setup(String address) {
bt = new Bluetooth(this, handler);
outStringBuffer = new StringBuffer("");
bt.startConn(address);
//Java throws error that bt is non-static
}
So my questions:
Is there a way to call setup() from another class when it is non-static?
And if answer is YES how? Or if answer is NO How to pass data without calling any methods.
Keep in mind that startActivityForResult() or startActivity() will not solve this problem, because I need to send data from Activity B to A many times and Activity A should call method from my bluetooth class which sends that data via bluetooth.
P.S I found a way with ViewFlipper, but in my case this is too complicated to merge two classes.
And I cant send data directly from Activity B to Bluetooth class, because Activity A has already opened socket and Java will throw exception that it cant start activity or something like that.
OK that's very nice...to pass strings from activity 1 to activity 2 you need
A method to pass and retrieve this, Intents
public void setUp (String address) {
/* start an intent to pass the string data*/
Intent intent = new Intent(GridViewActivity.this, MovieDetailActivity.class);
/* use the intent object to pass string to another activity using putExtra method */
intent.putExtra("your string");
start intent(intent);
}
Then in your receiving activity use getStringExtra
final String original_title = getIntent().getStringExtra("your string");
then you can use this variable pass from activity using Intent and PutExra and getIntent and getStringExtra methods
You can google more on this. Passing data from activity using intents.
I'm developing two android applications let's call them app A and app B.
App A opens app B through and Share Intent and pass it params through a bundle, until this point it's ok, works.
Now, I want to realize some operations in app B and once these have finished close app B and return a message that operations has finished and the result of these operations to app A but I don't know how to solve it or how looking for.
I have thought another intent but it will reopen my app and delete the content of my screen that I have previously. Any suggestion?
Try to use ResultReceiver:
https://developer.android.com/reference/android/os/ResultReceiver.html
Or Messenger:
https://developer.android.com/reference/android/os/Messenger.html
I worked with ResultReceiver only, when developed SDK for one application. In first app you create an instance of ResultReceiver:
public ResultReceiver generateParcelableReceiver(ResultReceiver actualReceiver) {
Parcel parcel = Parcel.obtain();
actualReceiver.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
parcel.recycle();
return receiverForSending;
}
and then pass it as an extra parameter to Intent:
requestLogin.putExtra("EXTRA_TARGET_RECEIVER", mAndroidUtils.generateParcelableReceiver(emptyReceiver));
Where emptyReceiver is:
ResultReceiver emptyReceiver = new ResultReceiver(null) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
//do nothing
}
};
As you can see, it receives Bundle. And in your second application, retrive the receiver, and pass the result when needed.
NOTE
Please, make sure you use android.support.v4.os.ResultReceiver, because there is another implementation of it(with different package name), and it doesn't work very well. This implementation contains in:
compile 'com.android.support:appcompat-v7:23.1.1'
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
I know there have been a lot of posts related to this:
How to know which application the user chose when using an intent chooser?
https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED);?
Capturing and intercepting ACTION_SEND intents on Android
but these posts are somewhat old, and I am hoping that there might be some new developments.
I am trying to implement a share action without having it be present in the menu. The closest solution to what I want is provided by ClickClickClack who suggest implementing a custom app chooser, but that seems heavy handed. Plus, it seems like there might be some Android hooks to get the chosen app, like the ActivityChooserModel.OnChooseActivityListener.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
sendIntent.setType("text/plain");
Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));
ShareActionProvider sap = new ShareActionProvider(this);
sap.setShareIntent(sendIntent);
sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
System.out.println("Success!!");
return false;
}
});
startActivityForResult(intent, 1);
As of API level 22 it is now actually possible. In Android 5.1 a method (createChooser (Intent target, CharSequence title, IntentSender sender)) was added that allows for receiving the results of the user's choice. When you provide an IntentSender to createChooser, the sender will be notified by the chooser dialog with the ComponentName chosen by the user. It will be supplied in the extra named EXTRA_CHOSEN_COMPONENT int the IntentSender that is notified.
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
That is not possible.
Other "choosing" solutions, like ShareActionProvider, may offer more. I have not examined the Intent handed to onShareTargetSelected() to see if it contains the ComponentName of the chosen target, though the docs suggest that it should.
And, if for some reason it does not, you are welcome to try to fork ShareActionProvider to add the hooks you want.
The reason why createChooser() cannot be handled this way is simply because the "choosing" is being done by a separate process from yours.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
ShareActionProvider goes in the action bar. You cannot just create an instance, call a couple of setters, and expect something to happen.