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

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")

Related

How to disable home button of Android device in lollipop

I want to intercept home button click of Android device in lollipop version.
I think this can be done in another style if suits your requirement. By
creating your activity as home activity. If you want to disable home button
and show your custom application activity as launcher when home button is
pressed. Just add these lines in manifest for that activity for which you want your launcher.
<activity
android:name="com.example.TempActivity"
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:stateNotNeeded="true" >
<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>
</activity>
if user presses the home button, Android will ask for which launcher you want your home. Then your have to select your application launcher ALWAYS not ONLY ONCE.
if you want fully disable the user, so that he cant move to another screen
then set theme to fullscreen with NoTilebar.
Use this method:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
};

Acitivity SingleTask behaviour with SplashScreen

In my App I have:
-SplashScreen (SSc) preparing the application (starting services and so on)
-MainActivity (MA) the most relevant part of the app handling most actions
-and some other activities which are not that relevant
For my App I'd like to have the behavior like launchMode singleTask, so that my App is always started as a new Task, even when opened through a link click in SMS/EMail app. The best would be to have only one Instance of my Activities as they are all serially navigable.
However when I start SSc as singleTask it is the root of the stack and navigating to the MainActivity, pressing home, click on the Launcher icon again the app is fully restarted. So SSc is shown again and so on. In this Situation, I would like the MainActivty to be brought to the front instead.
my wish would be:
launcherclick -> SSc ->MA ->HOME -> launcherclick -> bring MA to front -> HOME-> relaunch from recents -> bring MA to front
Click on link ->SSc/MA (whether it is first start) with the same instances
In my App it does not make sense to have multiple instances, as the background service only handles one MainActivity at a time because it polls data frequently just for the seen "Thing".
Do you have any recommendations to achieve this goal?
my first idea was a LauncherActivity with launchMode singletask without layout to route the intents to the other activities (which most likely will be singleTop !?, because its only in one task then) like:
public class LauncherActivity extends Activity {
private boolean firstStart = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if(firstStart){
startActivity(new Intent(this, SplashScreen.class));
firstStart = false;
} else {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
}
}
Manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.startintenttest">
<application
android:allowBackup="true"
android:allowTaskReparenting="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name="x.startintenttest.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"></activity>
<activity
android:name="x.startintenttest.MainActivity2"
android:label="#string/title_activity_main_activity2"></activity>
<activity
android:name="x.startintenttest.SplashScreen"
android:label="#string/title_activity_splash_screen"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*.xyz.de"
android:pathPattern="/...-........."
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Its simple, I also did the same. I was using singleTask for my splash and the main activity. So that I faced the same issue(Splash was showing at every wakeup). But I solved this by removing the singleTask from the splash and kept it for the MA alone(I used to finish the splashActivity when the MA starts). Try this trick.
The solution provided Sripathi fixes the issue, but you end up with the link-opening-app having the splash screen layout in its preview. My solution to this is to have a LinkEntryPointActivity with no layout that then delegates the received intent to the splash screen.
class LinkEntryPointActivity : MyBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intentClone = intent.clone() as Intent
val componentName = intentClone.component
val newComponentName = ComponentName(componentName.packageName, SplashActivity::class.java.name)
intentClone.component = newComponentName
startActivity(intentClone)
finish()
}
}
And declare it in AndroidManifest.xml as
<activity android:name=".ui.LinkEntryPointActivity"
android:launchMode="singleTask" <-- This forces to open the link in a new task
android:screenOrientation="portrait">
<!-- Intent filters here -->
</activity>
The original splash screen activity should still be handling the MAIN action in the LAUNCHER category as usual.

"invisible" toggle app without any window shown

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

How to achieve one installed app, two launcher icons?

I want to achieve the goal that after installing an application, two icons appear in the Launcher. Clicking on either one of them will launch the corresponding activity. With the XML and JAVA code at the bottom, everything seems to work fine, except one case:
BUG:
Step 1. Click icon 1 to start activity 1
Step 2. Click Home icon to swtich to Launcher
Step 3. From Launcher, click icon 2
Expected result: Activity 2 gets started.Actual result: Activity 1 gets resumed.
Notice that in step 2, if I click the Back button instead of the Home button to get back to he launcher, then step 3 would succeed. But if I used the Home button, then the bug happens. Could someone please tell me what did I mess? Much appreciate!
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity
android:name=".TestActivity1"
android:label="Test 1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestActivity2"
android:label="Test 2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
// The first activity
public class TestActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
// The second activity, it uses a different content view
public class TestActivity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
Give your activities different task affinities, so they are independent.

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