how do i achieve ActivityOptions.setLaunchDisplayId(1) marshmallow in android? - java

I want to open another app in a second screen(POS) so kindly help me to run another app in side second screen. I got the code for Oreo but I need this to work from Lollipop to latest.
ActivityOptions options = null;
Intent launchApp = MyApplication.getInstance().getActivity().getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
launchApp.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
options = ActivityOptions.makeBasic().setLaunchDisplayId(1);
MyApplication.getInstance().getActivity().startActivity(launchApp, options.toBundle());
}
}

Android did not support showing multiple apps very well back on Android 5.0. All that you could do was:
Have one app use both screens, either by default mirroring or by using Presentation; or
Use something like my PresentationService to have your app use the secondary display from the background, while a "regular" app uses the primary display
You may wish to discuss your ideas with the device manufacturer, to see if they have other device-specific alternatives.

Related

SpeechRecognizerListener giving Bundle[EMPTY_PARCEL] in onResults(Bundle results) majority devices which have android 11

I am Using
speech = SpeechRecognizer.createSpeechRecognizer(getReactApplicationContext().getApplicationContext());
speech.setRecognitionListener(VoiceAppModule.this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 100000000);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.languageacademy");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 10000);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 10000);
The Above code for Speech recognition.
#Override
public void onResults(Bundle results) {
in about On Result the result is giving Bundle[EMPTY_PARCEL] in Result.
ArrayList matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
I tried in many of the Devices it is mainly giving problem in MI phones having android 11 and also some samsung phones.
Same problem here, this bug started to occur like few days ago.
I think the reason of this bug is the new version of "Google"
app (https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox), as SpeechRecognizer uses this app to recognize voice.
If you click uinstall "Google"
app, it will roll back to older version and onResult callback will work fine.
Solution that fixed my problem was removing:
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS extras
Note that it is extremely rare you'd want to specify this value in an intent. Generally, it should be specified only when it is also used as the value for EXTRA_SEGMENTED_SESSION to enable segmented session mode. Note also that certain values may cause undesired or unexpected results - use judiciously!
https://developer.android.com/reference/android/speech/RecognizerIntent#EXTRA_SEGMENTED_SESSION
I confirm the answer gave by Seba, only in my case I have to remove also
RecognizerIntent.EXTRA_MAX_RESULTS RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
and affected also Huawei and Samsung devices with Android 9, so it seems the issues's propagating day by day.
I was also facing the same issue. After removing the below intent it started working.
putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 3000)
Previous intent :
recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en")
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH
)
putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 3000)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1)
}
Modified Intent:
recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en")
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH
)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1)
}

Record a call through MIC on Samsung devices (Android 7.0)

I've simple Android app that record a call and it works fine on Android 6 + Samsung devices (both with MIC source and VOICE_COMMUNICATION). But once Samsung devices updated to Nougat (for ex. S7 / S7 Edge) these methods to record a call failed :( MIC record only my voice but not opponent voice and VOICE_COMMUNICATION does not works at all.
Could anybody advice what can be done here?
same problem.
still looking for the answer.
I've got a samsung s7. Firstly, I'v unlocked native recorder (in dialer) and it's works.
So, android 7 (and samsung) can record calls without inner actions inside of Android.
But, in the same time I've read that developers can't get the access to recording calls from higher level.
It seems like there's only one way to achieve our goals: modification of stock Android's version or catch sound stream before it gets to speaker.
Fix me if I wrong.
Normally for recording phone call, we can use 4 types: DEFAULT, MIC, VOICE_CALL, VOICE_COMMUNICATION. But with 3 types below is enough for all
MIC, VOICE_CALL, VOICE_COMMUNICATION
base on the android version of your phone that will support or don't support some of them.
So to make your app work perfectly with all android versions we should change the AudioSource type following the android version with this rule:
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
callType = "VOICE_CALL";
} else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
callType = "MIC";
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
callType = "VOICE_COMMUNICATION";
}
I am using this on my application and it worked on most devices. It's no need to add any C library.
Check my example application to see how they react with a dedicated Android version.
Github: https://github.com/tntkhang/call-recording-master

launch two apps, Android nougat

I want to create an app for android nougat, when I click on a button I launch two apps at the same moment and the same screen.
I want to use this new feature of Android 7, Is it possible?
You can use Accessibility API for such feature. It doesn't require any permissions.
android.accessibilityservice.AccessibilityService has following apis:
service.performGlobalAction(GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN) which you can use to initiate split screen mode.
public List<AccessibilityWindowInfo> getWindows () to check wether split screen mode is on. Look for a window with AccessibilityWindowInfo.TYPE_SPLIT_SCREEN_DIVIDER
You also will need to play with intent flags when launching activities.
val options = ActivityOptionsCompat.makeBasic().toBundle()?.apply {
putInt(
ActivityOptionsFlags.KEY_LAUNCH_WINDOWING_MODE,
ActivityOptionsFlags.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
)
putInt(
ActivityOptionsFlags.KEY_SPLIT_SCREEN_CREATE_MODE,
ActivityOptionsFlags.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
)
}
startActivities(listOf(intentBottom, intentTop).toTypedArray(), options)
Using this accessibility apis and intent flags you can achieve your goal. Consult this repo by stavangr for detailed implementation.
https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

Get User Android Version

is there any way for my application to get the user's Android Version or API Level ? .. because i am working on an app that supports API 9 (2.3 Gingerbread) .. and since API 9 does not support DialogFragments even with android.support.v4.app.DialogFragment imported, i decided to do an if else statement instead
if (the users Android Version is Lower than API 11)
{
i will show a new class instead of a Dialog Fragment containing all information about the developer
}
else if (the users Android Version is Higher than or equal to API 11)
{
my dialog fragment will show up containing all information about the developer
}
i hope anyone can help me out about this, thanks :)
Use Build.VERSION.SDK_INT to get the API level. You can then compare it against values in Build.VERSION_CODES:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// do something cool
}
Standard how to execute code for different versions.
Android provides a unique code for each platform version in the Build constants class. Use these codes within your app to build conditions that ensure the code that depends on higher API levels is executed only when those APIs are available on the system.
#SuppressWarnings("deprecation")
// We use the new method when supported
#SuppressLint("NewApi")
// We check which build version we are using.
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} else {
mapView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
Try this:
Build.VERSTION.RELEASE
http://developer.android.com/reference/android/os/Build.VERSION.html

Possible to skip track from an Android application?

I'm planning on doing a application for Android 2.1 that changes song every minute (through what I hope exists in Android, "next") for the application using the audio device atm.
So if I have Spotify running in background already, playing music, can I through my program change to the next track?
Let me know if I was unclear about anything.
Thanks in advance!
I know this is a bit old question, but it took me some time searching something other then what is mentioned here.
There is a workaround - broadcasting media button action. There is one catch - receiver can recognize if the broadcast was from system or from another app, so they can ignore the non-system broadcasts.
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
}
There's no universal audio transport API for music applications, so you'd need to see if the music applications you're targeting publicly expose service bindings or intents. If not, you won't be able to do this.
Just posted a relevant answer here
Using the AudioManager's dispatchMediaKeyEvent() method with a defined KeyEvent worked for me using the latest SDK.
The system music homescreen widget sends this intent for the built-in music player:
final ComponentName serviceName = new ComponentName(context,
MediaPlaybackService.class);
intent = new Intent(MediaPlaybackService.NEXT_ACTION);
intent.setComponent(serviceName);
pendingIntent = PendingIntent.getService(context,
0 /* no requestCode */, intent, 0 /* no flags */);
views.setOnClickPendingIntent(R.id.control_next, pendingIntent);
But it looks like this might take some hackery to implement outside packages in the music app itself because the MediaPlaybackService only accepts explicit Intents and isn't accessible from the outside. This thread seems to indicate it's possible with a bit of hackery, though.
But even then, as Roman said, not every music player will respect that Intent. You'll have to check with Spotify/Pandora/Last.fm themselves and see if they have any available intents to bind like that.
Looks that it's possible to use AudioManager to inject media keys.
Here is a snippet from another question
this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
long eventtime = SystemClock.uptimeMillis();
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);
The same way you can inject PlayPause button and some others.
I've tested it within a background service controlling Youtube and it worked for Android 6

Categories