Run application in the background - java

I'm trying to start the GroomDroid web server in Android but when it starts, a black window is shown.
I'd like to start this app in the background without that screen being shown.
I'm starting this app in the following way:
Intent webServer = new Intent();
webServer.setClassName("net.allory.groom","net.allory.groom.GroomDroid");
startActivity(webServer);
I also tried with startService(webServer); but it doesn't seem to work.
Can anybody help me with that ?

You should move your background logic to a service, and when it's running create an ongoing notification.
Selecting that notification should start your activity (ui), and the service can communicate its data to it using a Handler.
Also, if you specify the launchMode of your activity to singleTask:
<activity android:name=".activity.YourActivity"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:clearTaskOnLaunch="false"
android:finishOnTaskLaunch="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and on application start check if your service is already running (say you've already started the application, and now press its icon again), you'll be able to start the very same task, instead of relaunching your service and/or showing nothing.
This explanation might be not too clear, let me know if you need more details :)

Lince,
Looks like the emulator. If you are using eclipse go to the 'Run Configurations...' and change the 'Launch Actions' to 'Do Nothing'
Do you want the app to always be headless?
Frank

Related

How to support shortcut pinning in a custom Launcher?

I'm making my own Android launcher and I want to react to requests from other apps to add a shortcut to home screen.
So far, I was only able to find out, that apps have to check if shortcut pinning is supported by calling isRequestPinShortcutSupported() from ShortcutManager and then send a request by calling requestPinShortcut() - but how do I implement the other side of that?
I don't know how to tell ShortcutManager my launcher wants to receive these requests and where to handle them. I have tried registering various BroadcastReceivers but that wasn't enough.
Only helpful thing I could find was LauncherApps.PinItemRequest, that can be created from Intent, but I can't find how do I receive this intent.
Okay, I had to study android source code for this but I figured out I need to create an Activity like this:
<activity android:name=".Activities.AddShortcutActivity">
<intent-filter>
<action android:name="android.content.pm.action.CONFIRM_PIN_SHORTCUT" />
</intent-filter>
</activity>
And then in that Activity's onCreate() I can call
LauncherApps().getPinItemRequest( this.getIntent() );
//handle request...
this.finish();
and do whatever I need with that request. Hope this helps someone.

How to customize Android Google Setup Wizard for device owner provisioning?

--Use case:
1-System app apk in priv-app folder to be used as device owner.
2-User starts up device and Google setup wizard comes up.
3-Immediately starts device provisioning activity.
--Things that used to work:
This method used to work on Android 6.0 Marshmallow using the action intent:
<activity android:theme="#style/InvisibleNoTitle" android:name="OwnerActivity" android:launchMode="singleTop" android:immersive="true">
<intent-filter android:priority="5">
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DEVICE_INITIALIZATION_WIZARD" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
without any problem.
After updating to Android 8.1 Oreo, this method no longer works. The OwnerActivity shows up only after setup wizard finishes which is useless since device is already provisioned by user and can't be provision again.
Is there any newer way of doing this so that my OwnerActivity shows up first to provision the device? What is changed in Oreo?
This is a bit late for an answer, and without talking to Google it's a little hard to see what the design decisions have been. However, what we can know is what's changed.
1. android.intent.action.DEVICE_INITIALIZATION_WIZARD is deprecated atleast as early as Oreo.
2. Package Manager Service has had changes to reference the setup wizard. The new approach appears to be the category android.intent.category.SETUP_WIZARD which your manifest definition lacks.
Reading the comments around the code (which you can find below) we see this log:
Slog.e(TAG, "There should probably be exactly one setup wizard; found " + matches.size()
+ ": matches=" + matches);
https://android.googlesource.com/platform/frameworks/base/+/nougat-mr2.3-release/services/core/java/com/android/server/pm/PackageManagerService.java#17872
So it seems like as of Nougat, Android doesn't support having multiple setup wizards that are chained together.
For your specific problem of how to setup the device admin I have 2 suggestions.
If you care about CDD and CTS then you should get your users to go through the Google device owner provisioning process which they keep updated.
If you don't care about that and you just want your build to always have a device owner, just make changes in the frameworks or add some system binary that always runs before the setup wizard which will set your device owner application for you.

Open Android App button does not return to the current Activity already open

See Update 2 for current Problem regarding of Activity Launch Mode
I am developing an Android app. Now I have the problem that once I open a new activity (that is not the activity with android.intent.action.MAIN and android.intent.category.LAUNCHER) in the app, and then press home button, and then press the app icon, the app does not navigate me back to the new activity I have already open (and shown on recent app list). Instead, it open a new MAIN activity.
What should I do to change this behaviour?
===================================================
Update
Code for Start Activity Other from Main Activity
startActivity(new Intent(MainActivity.this, LocalActivity.class));
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:theme="#style/AppDrawTheme"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LocalActivity"
android:launchMode="singleInstance"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait">
</activity>
===================================================
Update 2
I solve the problem by change
android:launchMode="singleInstance"
to
android:launchMode="singleTask"
Although I do not understand why singleInstance would be a problem.
How should I understand what Google write on the Android document "except that the system doesn't launch any other activities into the task holding the instance" ?
Most probably you are setting Intent flags that cause this when you call startActivity() or you use activity's properties in AndroidManifest.xml that cause this. I would need to see your manifest or code to point where is the problem exactly.
Updated
Google docs also says:
The activity is always the single and only member of its task.
You should go through this page and read carefully about tasks and activities.
Basically, when you start your main activity it's created within new task. Next activities are placed in the same task(in the stack). Now, the crucial part, Android does not restore only single activity but entire task. This is default.
In case you use singleInstance every Activity has own Task and when you start app again(after HOME button) Android restore Task with root Activity, not the one with your last seen Activity. Be very careful when you change launchMode, if it's not clear how it works then you should probably find it out first, otherwise you may have more surprises in the future.
I hope it's clear now for you, if not please read linked documentation.
You should use onPause() and onResume() in your activity.

Android app deploy on eclipse creating duplicate

Whenever I Run an Android app on my phone (even when the application has not been installed before), the application is shown duplicated in the app drawer. This started happening right after I setup a Splash Screen activity, and I find it kind of annoying. One of the apps in the launcher install with the SplashScreen Activity, the other one does not. The app works without issues, but is there a way to remove this duplicate app icon in the drawer?
It's not urgent, but maybe you guys know ;)
I run my Android app projects with Eclipse, if it's of any help.
Any ideas? Oh, and if you guys need me to show you some of my code, please tell me.
I followed this tutorial for the creation of my splash screen.
Be sure to only have this:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
at one Activity, in your manifest.xml

Android - How to download data in the background at specified times

I'm sorry in advance for not having any code to post up, mainly because I can't for the life of me figure out how I need to do what I need to do.
Basically, at specified intervals during the day (ex. 5 P.M), I want my app to download some data from my server and store it on the device. This is to both reduce the load on my server from having data being downloaded every time the app is run, and to reduce the loading times for the user so that when they go to use the app, the latest data is already sitting on their device.
I have absolutely no clue how to do this. I know how to download data just fine, but now how to download in the background like I'm planning. Is it even possible?
I'm not asking for anyone to do it for me, but could someone please point me in the right direction.
Use the AlarmManager
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
Use it to start a Service
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
The API Demos includes an Alarm Service example (in the "App" section), which:
Demonstrates how you can schedule an alarm that causes a service to be started. This is useful when you want to schedule alarms that initiate long-running operations, such as retrieving recent e-mails.
In particular, see AlarmService.java for an example of using AlarmManager to schedule your Service to be woken later, and see AlarmService_Service.java for an example of how to respond to that alarm. The API Demo's AndroidManifest.xml contains the related service and activity definitions:
<service android:name=".app.AlarmService_Service" android:process=":remote" />
<activity android:name=".app.AlarmService" android:label="#string/activity_alarm_service">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
Write a Service.
Use the AlarmManager.
could someone please point me in the right direction.
AlarmManager, Service, AsyncTask, BroadcastReceiver
<receiver android:name=".receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Categories

Resources