Android not detecting when device rotated - java

I am trying to start a new activity when an Android device is rotated, but I don;t even seem to be detecting the rotation in the emulator.
I've read the thread at Android: listen for Orientation change? and that all seems to amke sense, but its just not working.
In my manifest I have:
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and in my mainActivity.java I have:
#Override
public void onConfigurationChanged (Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation=newConfig.orientation;
switch(orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
showMessage("landscape");
break;
case Configuration.ORIENTATION_PORTRAIT:
showMessage("portrait");
break;
}
}
This obviously won't start the new Activity, but I am trying to get the orientation detection working first (showMessage just calls a Toast and is working elsewhere in my code, so that's not why I am not seeing anything).
When I run this in the emulator and use the rotate buttons, the emulator rotates as expected but I never see the Toast...
Where am I going wrong? (I am importing android.content.res.Configuration as required for the Configuration constants).

onConfigurationChanged method will not be invoked when you rotate your device, actually, nothing will be invoked because of this line:
android:screenOrientation="portrait"
device is just locked in a portrait mode, remove this line, and onConfigurationChanged method should be invoked.
If you want to detect that device is rotated and keep android:screenOrientation="portrait" line you can use accelerometer sensor.

Try after removing this lines
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
because you are locking screen orientation in manifest and you are
also overriding on config change in activity.

Related

How do you change the default dialer package in android studio?

I'm currently trying to change the default dialer package of the machine to my application. When running the code that is supposed to change it, however, it results in the default dialer package retaining a value of NULL. This is loosely following the guide here, but some of it is deprecated so I've had to make some adjustments.
EDIT: has REQUEST_CODE_DEFAULT_DIALER been changed somehow? I can't find it referenced anywhere other than outdated tutorials on how to do this, so I'm not sure what it would even have been changed to. I'm getting a "cannot resolve symbol" error whenever I try to use it.
I've included the following under the relevant activity in my AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme = "tel"/>
</intent-filter>
The following code executes the package change and then prints its value to logcat so I can see what happens:
Log.i("Before", "Before default dialer change");
startActivity(new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,getPackageName()));
TelecomManager telecomManager = ((TelecomManager) getSystemService(Context.TELECOM_SERVICE));
if(telecomManager.getDefaultDialerPackage() == null) {
Log.i("Default dialer package:", "NULL");
} else {
Log.i("Default Dialer Package:", telecomManager.getDefaultDialerPackage());
}
I'm getting "Default dialer package:: NULL" in logcat every time I try this.
Intents are asynchronous. If you start an activity to do something, you have to wait for that intent to finish before using the results of it. You're instead immediately querying for the default dialer package. That will always fail, because it wouldn't even have shown the picker on screen yet.

Android createChooser appear in front of Activity

I'm hoping one of you fine folk may be able to help me out with a little issue I am having whilst using Android Intent.createChooser(). I am learning as I go with my application so I apologise if I have the wrong end of the stick with how this should work!
I have been following a number of tutorials and have implemented some code which starts an Intent.createChooser() to share a picture to social media. The code that I have used works fine in that I am able to share the picture as I intended, my issue is that when the code is run my main activity (or any activity I call the code from) appears to close and the chooser appears in front of the homescreen or any other applications I may have open. The effect that I was hoping to have, and the one which seemed to be apparent in the tutorials, is for the chooser to appear in front of the running activity (eg as it does for the gallery).
I am using LibGDX meaning that I have had to use an interface to call the android function that executes the chooser code (in my AndroidLauncher java file). The interfaces all work fine but I wasn't sure if that may have a bearing on the way in which this code operates. I have also played around with swapping to a new activity to implement the chooser code, which I had moved from the AndroidLauncher. This had the same end result.
Any advice on how to make the chooser appear in front of my application would be greatly appreciated! The code which implements the createChooser is included below:
public void ShareHandler(){
String type = "image/*";
String mediaPath = Environment.getExternalStorageDirectory().getPath()+"/Smile.png";
createShareIntent(type, mediaPath);
}
private void createShareIntent(String type, String mediaPath)
{
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(type);
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share to"));
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme">
<activity
android:name=".AndroidLauncher"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FacebookServiceHandler" />
<meta-data
android:name="come.facebook.sdk.ApplicationID"
android:value="#string/facebook_app_id" />
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProviderxxxxxxxxxxxxx"
android:exported="true" />
</application>
</manifest>
Thanks in advance, Ben.
If
android:name=".FacebookServiceHandler"
is the activity, that handle the social media sharing of the picture, i would expand that to something like this:
<activity
android:name=".FacebookServiceHandler">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".AndroidLauncher">
</meta-data>
</activity>
This will tell the programm, that the AndroidLauncher is the parentactivity and should make it appear in front of your activity.
Thank you for the suggestions, I have now solved my issue. It turned out that my code was working correctly all along and the issue I was experiencing was as a result of an exit statement within my Core (LibGDX) java code. Essentially when the startActivity(Intent.createChooser(...)) was called, a dispose function within my core files was also being called. This dispose function contained the exit command and was causing the application to finish.

Android app opening wrong page after using uri scheme

In IOS I have 2 apps (App A and App B) that will be performing different objective but at 1 point, App A will call App B using uri scheme to perform a function where App A does not have.
I wanted to implement this function to Android and currently have developed a demo for initiating call to App B. Now I am working on App B that will be receiving the uri scheme from the demo app.
Problem:
After App B return to the demo app using uri scheme I close the demo app.
When I go back to home page and open the multitask to reopen App B(or open App B through the icon), the page it reopen is the function that is used for demo app but what I wanted is the login page of App B.
This is a part of the Manifest for the intent-filter
<activity
android:name="com.apps.MasterActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:clearTaskOnLaunch="true"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data
android:host="x-callback-url"
android:scheme="myapp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
This is where I call back to the demo app
Intent sendIntent = new Intent(Intent.ACTION_VIEW, responseUri);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(sendIntent);
Try adding this to your activity tag in AndroidManifest.xml
android:clearTaskOnLaunch="true"
Apparently I kinda found a solution that can force the app to show the login page after going back to the demo app. By putting this code in the activity.java file.
#SuppressWarnings("deprecation")
#Override
public void onDestroy()
{
super.onDestroy();
System.runFinalizersOnExit(true);
System.exit(0);
}
I know its a deprecated function that I'm using but will try to solve it when there's something wrong in the future or when I found a better solution for it.
Edit:
Found a better solution that does not need to use the deprecated function which I have forgotten to set it to false.
I have a flag that is set to true when the app is connected through Uri Scheme which i just have to set it to false and it will be fixed. This is the change location that I made
Intent sendIntent = new Intent(Intent.ACTION_VIEW, responseUri);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(sendIntent);
Util.isUriScheme = false;

Confusion over execution order

I have a game application made up of several different activities. The first to be called is a splash screen, when this completes, this finishes and starts up another activity via an intent. In order to have access to some global data that is consistent across all the activities, I also have a "globals" class like this:
public class Globals extends Application
{
int global_variable_A;
int global_variable_B;
int global_variable_C;
public void onCreate()
{
// stuff
}
}
In the androidmanifest.xml I have the the following (amongst other things):
<application
android:icon="#drawable/mygame_icon"
android:screenOrientation="portrait"
android:label='"My Game"' android:name=".Globals">
<activity
android:label="My Game"
android:name=".Splash"
android:screenOrientation="portrait">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My question now is, which will be executed first, the onCreate of Globals or the onCreate of Splash? Or are they run at the same time on different threads? I ask because I'm getting some inconsistent behaviour that would be explained if they were on different threads.
onCreate() Global off course.. Application gets executed first and then the Activity,,.. you can test for yourself by keeping a debug point in Application onCreate() method..

Restore android app stack from background

Lets say I launch my app from the home screen, navigate through some activities, then I press the home key and do something else in the Gmail app.
After i'm done checking my mail,I press the home key again to leave the Gmail app and click my app's icon at the home screen again to return to it.
When I return to my app, I want it to return to the last activity I was at, NOT start a whole new session. I've been trying to figure this out all day.
My manifest for my first activity is as follows:
<activity android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The category attribute LAUNCHER makes my app ALWAYS start at activity Main, so I don't know how to go about restoring the last activity. People have told me to use sharedpreferences to save the last activity and load it on Launch, but I don't think it's intended to be done that way because it isn't very elegant.
I think its the only way because what happens when you're launching an app is that Launcher application sends intent "android.intent.action.MAIN" And the only activity in your app that responds to this intent is your main activity, thus it gets activated. So the only thing you can do is save somewhere your session and on activity start up if there's already saved session restore the app to the previous state.
Try using one of these in your manifest :
<activity android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"] ...
Are onResume() and onPause implemented properly?
protected void onResume(){
super.onResume();
}
protected void onPause() {
super.onPause();
}

Categories

Resources