Bluetooth discoverability request and screen orientation - java

Asking user the permission to turn device discoverable with the following snippet
final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent, BT_REQUEST);
causes the following system dialog to show up in portrait mode while my calling activity is in landscape, which is not a good user experience.
Many landscape only bluetooth apps I tried from the Play Store seem to suffer the same problem.
Is there any way to force the dialog to appear in landscape mode?
I tried to override system activity orientation from manifest, without success
<activity android:name="com.android.settings.bluetooth.RequestPermissionActivity"
android:screenOrientation="landscape" />

Related

Android: playing video in picture-in-picture mode ( multiple instances when click on close button)

For my android application, when a user play a video, a new activity (VideoPlay) starts in pip mode.
Manifest (Video Play)
<activity
android:name=".PlayVideo"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar"
tools:ignore="LockedOrientationActivity"
android:supportsPictureInPicture="true"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation"/>
The problem is when i click on close button "X" of the pip screen, a new instance of my app is created.
how to handle this problem ? thank you
That is not another instance of your app, but rather a different task which houses your PlayVideo activity. To prevent showing your activity in recents screen, you can add android:excludeFromRecents="true" in the manifest attributes of your PlayVideo activity.
Also, if you're force closing your PlayVideo activity in some way other than user interaction like backpress, you may like to call finishAndRemoveTask() instead of just finish() as calling finish() will not remove your activity from recents.

Why finish() make all the activities can't go back to previous activity?

I'm new in Android Java and I have some question about the intent. This is the intent concept of the app
First, The Launch Activity have two button to let the user push the screen to Register Activity and Login Activity . Other than that, User also can press the back button to go back the Launch Activity. So it is not include finish().
//Login Activity
Intent i = new Intent(LaunchActivity.this, LoginActivity.class);
LaunchActivity.this.startActivity(i);
In the AndroidManifest, Main Activity (yellow) is the first activity.
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
On Main Activity, I will check if whether the user is login to the app or not. If haven't login, I will push the screen to Launch Acitivty by using this code. (I'm using this code on the sign out button too)
Intent i = new Intent(MainActivity.this, LaunchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
So I use finish() to prevent the user go back to the Main Activity.
But the problem is the finish() also let the Login and Register Activity can't go back to the Launch Activity.
So is the finish() make all the activities can't go back to previous activity? why and how to fix it?
Due to the comments, I know the problem is the flag. So I removed those flags when checking the login status and sign out. but after I login successfully (login screen to main screen), I can press the back button to go back to the launch screen on the main screen. why? It only happen when first login. after I relaunch the app, the app work fine. This is the code I use on the login button.
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
Is it because we can't destroy the launch screen on the login screen? but adding those flags should be destroy all the history right?

Open an image using URI is missing icons on top when displayed

I'm using the following code to display an image that it's stored on my sd card:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/pp.jpg"), "image/*");
startActivity(intent);
This works ok, but it displays something like this:
If I open the image directly from my phone, it shows like this:
How can I show the rest of the icons that you see on top when I open it from my app? I need at least to show the share button. Thanks
With this intent, you are telling the OS to find an app that can handle opening an image and you're doing this without checking whether there even is an app to handle it. A 3rd party app is thus handling the intent and opening the image taking it out of your control. You could try and find the right intent or extras to get the desired effect from the 3rd party app, but it is unlikely that you will be able to get it and furthermore there are thousands of devices in Android land and each one could have a different app handling your intent.
In order to ensure the functionality is as desired, you should open the image in your own app and create a menu with the options you desire as bhargav instructed.
Also, you should start your intent like the following to be a little safer with it if you are still going to have 3rd party apps open your images.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/pp.jpg"), "image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
You have to create new Activity with an ImageView in center. And add all your menu icons in your menu.xml file of this activity as below.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu1"
android:icon="#drawable/icon_menu1"
android:title="menu1" />
<item android:id="#+id/menu2"
android:icon="#drawable/icon_menu2"
android:title="menu2" />
.......
</menu>
If the activity that is showing the image is nor your activity, you have no control over what, if any, options that activity gives the user. That activity was written by another developer, and that developer is free to do what she wants.
Moreover, there are thousands of Android device models. There will be hundreds of different image-viewing apps pre-installed on those device models, and there will thousands more available for download from places like the Play Store. The user will choose the app to view the image. What options, if any all those apps offer will be up to the thousands of developers who wrote those apps, not you.
If you want absolute control, write your own image-viewing activity.

Home button Android 3.0, 4.0

I have an application in android that includes a application home page. I override the hardware Home button to go back to the application home page for API level 10 or less.
My application works fine on Android version 3.0 and 4.0. The problem is overriding the Home button. I found a discussion ( Disable Home Button in Android ICS (4.0) ) which includes a method to implement a home screen somehow. It is suggested by #Chalaman.
I did not get the point yet. Is there any one that can help me more by providing some codes?
when we use :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in the manifest file, a dialog pops up when we click on the home button. It includes the home phone page icon and application icon. we can make a choice. If we select the application icon, we stay in the application. The problem is we stay also in the same activity. How can I go to another activity (home page of my application)?
We can to it in API level 10 or less:
#Override
public void onAttachedToWindow() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
loadStartPage();
break;
}
return super.onKeyDown(keyCode, event);
}
private void loadStartPage() {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.putExtra("user", user);
intent.putExtra("user_id", user_id);
intent.putExtra("server", server);
intent.putExtra("password", password);
main_activity.startActivity(intent);
}
How to Load Start Page in API level 11 or greater?
The answer is simple: don't do it.
Users expect their apps to be consistent across the entire Android platform. I can't imagine a situation in which you could ever justify overriding the home button.
Edit:
To be clear, there are apps out there that appear to override the home button (i.e. the Car Mode app, which won't allow you to escape the app unless you click the "Exit" button). Apps such as these don't actually explicitly override the "home button". You can read more about it here.
the android home screen is just an app. it is started by a well-defined broadcast intent that is issued when the home key is pressed.
in a nutshell, you implement an android application that listens to the same event that the stock android home screen app listens to. when the user presses home, they will see the chooser allowing them to select from the stock home screen app, our your app. here's what the intent filter would look like,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
i realize that's probably not what you want. however, there's no way to completely override the home key. this is intentional as it prevents malicious apps from locking you out of the home screen. the home button is sort of the guaranteed to work "get me out of here" button.
your users can flag your app as the default to handle the home broadcast, but that's probably not what you want either because from then on they want be able to get to the stock home screen unless they clear default or uninstall your app.
preventing users from accessing the home button is a pretty nasty thing to do. are you sure that's what you want?
Override onKeydown can't solve your problem .. when you press home button activity calls onPause() .. so put your method inside of onPause().. I guess it will work ..
protected void onPause() {
super.onPause();
loadStartPage();
}
private void loadStartPage() {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.putExtra("user", user);
intent.putExtra("user_id", user_id);
intent.putExtra("server", server);
intent.putExtra("password", password);
main_activity.startActivity(intent);
}

hide keyboard in android?

I am new to android development.
I am creating an application in which when app launches the edittextfield getfocus automatically and the keyboard pop-up.I want to hide the keyboard by default and show when edittextField get focus. I have tried this method but it's not working.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Change your manifest file:
pput it there:
(on your especific activity)
android:windowSoftInputMode="stateHidden"
Declaring android:windowSoftInputMode="stateHidden" in corresponding activity tag on Manifest file will solve this. But study about this attribute here.
android:windowSoftInputMode

Categories

Resources