"invisible" toggle app without any window shown - android

I'm writing simple APN toggle app. I wanted to ask how to force android not show any window.
Currently after running my app, for brief time Black screen with app name is shown and then disappears.
Is it possible not to show anything ( only Toast message ) ?
public class ApnSwtichActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (toggleAPN()){
Toast.makeText(this, "Apn switched", Toast.LENGTH_SHORT).show();
}
this.finish();
}}

Sounds like you want an activity without a UI
How to launch an Activity without a UI?
You'll probably want to use
android:theme="#android:style/Theme.NoDisplay"

I don't think this is possible exactly as you want. You can hide title bar like this: add android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" in your manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TestActivity"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

You could create an activity, make it transparant and give it the parameters FLAG_NOT_TOUCH_MODAL & FLAG_NOT_TOUCHABLE
(FLAG_NOT_TOUCH_MODAL passes any touch input you give to your activity to the underlying screen, FLAG_NOT_TOUCHABLE negates any touch input commands for the activity)
Don't forget to make your activity autoclose after you're done

Related

black screen after press back button

My app contains only one activity and ads activity. It works well, but when I press "Back" button my application is closed and I can see, instead of wallpaper, black background which is partly rolled down. I can see all icons as well. Then black part of screen disappears and I can see my normal screen wallpaper. It takes only less than a second but it is annoying. I can't found what the problem is it.
My activity has only horizontal orientation and has correct life cycle (onPause-onStop-onDestroy) after pressing back button. Ads activity is not launched at all.
I did not override onPause() onStop() onDestroy().
My Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/icon_transp_bkg"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="MainActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize"
android:theme="#style/PlayTheme2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
Could it be related to back stack and trying to search some previous activity?
Please, put me on the right way to solve the problem. Thanks a lot in advance.
You can use OnBackPressed
#Override
public void onBackPressed() {
// what you want to do when back button is pressed
}
You can override the OnBackPressed method in the activity
#Override
public void onBackPressed(){
finish();
}
And call on the FINISH method, this will properly close both your activity and app.
Keep the Back-Stack and Trying to search some previous activity.
Ovverride the onBackPressed with a Call to Finish to End Both.
public void onBackPressed(){
finish();
}

How to show splash screen when app is in background in Android?

I have a Splash screen(Couldn't avoid it, as it is for branding reasons) for my application.
I want to mask the user interface and show the splash screen when it is in background (like banking applications do).
Should I overide onPause() and onResume() for the view in MainActivity?
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
Splash Activity :
onCreate()
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Main-Activity. */
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
MainActivity(): Just show a layout with text and buttons
onCreate():
setContentView(R.layout.example_view);
Now when my application is in background and When I press Menu button to see list of applications in my stack, I should see the splashview(the xml file) not the deafult behaviour i.e MainActivity layout.
One of the things I tried out is adding
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
which does mask the application when it is in background with White Screen but I'm trying to show the splash view when the app is in background.
I know all the iOS Banking application has this feature.
Edited :
If you see the twitter app(Example) in the background, I want to mask the application view with splash screen. Right now using FLAG_SECURE helps me to mask the screen with white screen but I want to show Splash layout rather than white screen.
If you see the below picture from an iOS device - DISCOVER application (example) masks the app with splash screen, Can this be done on Android?
Can this be done on Android?
Not directly, AFAIK.
Should I overide onPause() and onResume() for the view in MainActivity?
Probably not. While you are welcome to change your UI there:
Those methods get called for other reasons (e.g., when the user taps on another window in multi-window mode, when you launch another of your activities)
That may be too late with respect to when the screenshot is taken

How to run a certain activity in Android Studio?

For instance, I have a few activities within one app, and in order to see a certain activity's UI or whatever, I need to run a certain activity that is not the launcher of the app.
One stupid way is to build a "door" for that activity in the launcher and go inside the activity from the door. However, Is there any better way to run a certain activity alone?
Very easy. Start by exporting the activity you need to run:
Add android:exported="true" in the Activity declaration in the Manifest. This is because am is an external application, and you need to export Activities to allow external application to start them.
Go to "Edit Configurations..." in the "Run" menu.
In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown.
Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.
You can create as many Configurations as you like, and name them according however you like, for example to indicate which activity is being started.
I am using Android Studio stable version 2.1.2 and there is one shortcut to do so. Just open the activity class you wish to run and right click on coding area, There is options to run and debug the particular activity as shown in below screen shot.
For windows use shortcut ctrl+shift+F10 and for mac use ctrl+shift+R. I have tested this in emulator and its working fine, didn't test in actual device.Works only for activity class and don't forget to put cursor in coding area by clicking on it. Also I am not aware whether this option available in older Android studio versions less than 2.1.2.
As mentioned in this answer, you can easily achieve that by giving the activity an action name in the manifest.xml of the app:
<activity android:name="Activity3" ... >
<intent-filter>
<action android:name="com.company.package.FOO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
then create the following intent from anywhere in order to run this activity specifically:
startActivity(new Intent("com.company.package.FOO"));
After your clarification that the activity has to be run firstly when running the app instead of the launcher, you can achieve that by not setting the content of the launcher activity and instead create an intent that runs the wanted activity:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_layout); // remove this line
Intent intent = new Intent(ThisActivity.this, WantedActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Add exported true Manifest declaration of that activity.
Go to that activity, right click anywhere, go will get certain option with a 'Run XYZ Activity' option too. just run it
<activity android:name=".phoneVideo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
First you need to have two or more activities in your app to start with. Let's say you want to go to a certain activity in your app to display first. May be for testing purposes or any other. Let's see how it can be done,
First you need to find the AndroidManifest.xml file. Its there under the manifests folder.
According to this first dispaly activity is MainActivity
Lets's say I want to make the home activity display first. So what I have to do is simply cut the intent-filter.../intent-filter and paste it within home activity. Like this
First Displaying Acivity is MainActivity according to this,
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".home">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When we want to make the home acivity display first simplay change it as this,
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".home">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
This should work. Hope this will help
For my example, the specific activity was called Activity2 and the project was called ScreenSizes
1- Open the Android Manifest: app>manifests>AndroidManifest.xml
2- Change the activity section for the specific activity to include android:exported="true" like this:
<activity android:name=".Activity2"
android:exported="true">
</activity>
3- Open the java class of the specific activity: app>java>com.example.(your app name)>(specific activity)
in my case it was: app>java>com.example.screensizes>Activity2
4- Right click anywhere in the blank/white area of the Java file and select the option Run '(activity name)'
in my case it was: Run 'Activity2'

Close/Kill Android application from recent application on soft key press

I have a overlay view over all apps. I want to close the overlay view from screen on soft key press (home, back and recent). I have a transparent activity with no content which is starting a service having overlay view.
public class SampleOverlayShowActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, SampleOverlayService.class));
}
#Override
protected void onPause() {
SampleOverlayService.stop();
finish();
super.onPause();
}
}
Inside manifest
<activity
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="SampleOverlayShowActivity"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Dialog" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Everything is working fine expect recent apps. Activity still show up on recent application button press. It got removes when I again press recent application button.
This should be much simpler than trying to override soft key behaviour to do clean up. Instead make sure the activity is launched so that it has no recents trace. From the manifest that means adding noHistory and excludeFromRecents
<activity android:excludeFromRecents="true"
android:noHistory="true"
android:name=".."
...>
<intent-filter>
...
</intent-filter>
</activity>
Note that android:excludeFromRecents works for the task only, so you probably want to make sure that activity is a new task too, using the appropriate flags (android:launchMode="singleTask")

Android App - disappearance of app GUI

I'm trying to create a simple app, whose main task is to open the browser on defined URL.
I've created first Activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Intent myIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://my.url.tld"));
startActivity(myIntent);
}
Here's my AndroidManifest.xml:
<manifest ...>
<application ...>
<activity android:name=".MyActivity" ...>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This code is fully functional, but before it opens the browser, it displays a black background - blank app GUI. I didn't figured out, how to go directly do the browser (without displaying the GUI).
Anyone knows?
Add this theme declaration to your activity definition in you manifest
<activity android:name=".MyActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
>
Be careful with this, you must call finish(); from your activity after you are done with it otherwise users could get stuck with an invisible activity that prevents them from interacting the phonetop.

Categories

Resources