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

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>

Related

Tile only application

Having seen the questions about activities with no GUI here, here and here. In addition, these answer are rather old, I don't know if they're still relevant.
I want to create an app for which the only user interaction is a quick tile.
I understood there can't be no activity at all, so I have a blank activity with no display using #android:style/Theme.NoDisplay
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".AdaptativeSleepTile"
android:icon="#drawable/ic_launcher_background">
</service>
But the app appears in my app list (with nothing happening when I click it, logically), which is not what is written in the comments of this answer.
I can't remove the line <category android:name="android.intent.category.LAUNCHER"/> otherwise I get the error message
Could not identify launch activity: Default Activity not found
Error while Launching activity
So what should I do to have a service for which the only user interaction is the quick tile? (Same question would also apply for no interaction at all, or only widget I guess)
Using Android Studio 4 and Sdk 29
I want to create an app for which the only user interaction is a quick tile.
That may or may not be practical. Since Android 3.1, apps are installed in a "stopped state", and it takes an explicit Intent to move them out of the stopped state and allow them to run. Normally, that comes from the user tapping on an icon in a launcher to run your MAIN/LAUNCHER activity.
It is possible that simply having a TileService available in the manifest is enough to get you listed in the notification shade, and the act of adding the tile will be enough to move your app out of the stopped state. However, I certainly cannot guarantee that, and it would not surprise me if this does not work.
Also, please bear in mind that you may need an activity for other reasons:
To display your terms of service
To display your privacy policy
To provide access to tech support
To allow for configuration of the app
And so on
But the app appears in my app list (with nothing happening when I click it, logically), which is not what is written in the comments of this answer.
If you mean the first comment, that is simply wrong, as is pointed out by other comments on that answer.
I can't remove the line otherwise I get the error message
I assume that you are getting that from Android Studio. You will need to change your run configuration to not try starting an activity.
Same question would also apply for no interaction at all
Fortunately, there is no solution for that. Malware authors think that totally invisible apps are wonderful, and Android takes steps to prevent such apps from being able to work. And, this is why I would not be surprised if you need an activity for your TileService.

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.

Launch the Android App using deep linking

I'm trying to launch my Android App, using deep linking. Basically, the users will receive emails with links, when the user clicked the link, the App should launch.
I know how to do the basic deep linking, however, I want to launch the actual App not just a specific activity. My deep linking scheme is something like "mydeeplinking" and in the email is like "mydeeplinking://".
I am looking for Something similar to the iOS deep linking, which launch the entire App.
Any help would be appreciated.
Thanks in advance.
Basically, all you need to do is use intent-filter to tell Android what type of data should be routed to your app.
AndroidManifest.xml:
<activity android:name="com.example.MainActivity" >
<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:scheme="http" />
<data android:scheme="https" />
<data android:host="www.example.com" />
<data android:path="/" />
<data android:path="/map" />
</intent-filter>
</activity>
This will launch your MainActivity when the user clicks any of these links:
http://www.example.com/
https://www.example.com/
http://www.example.com/map
https://www.example.com/map
Deep linking is greatly enhanced by Firebase. It's a little hard to get started, but it works great! Check this link for an example.
Comparing this deep linking to iOS is where the confusion may come in, because theirs works differently. Android has always been able to push information from one app to another and even pull from the web, which is what iOS is doing. That isn't deep linking per-se. It can go MUCH further than that- it is meant to provide a personalized app experience even before a user creates a profile (or something similar). It's also important to note that deep linking holds onto this personalized info even through the installation of an app and opens the app with whatever that info was! It's not just an intent! Your question has to do with URI schemes. Maybe this will help if you still agree that you're looking for deep linking.
If you are simply looking to launch an app (already installed on the users device or not), intent filters are the way to go, and intents can pass information. If you are wanting a user to use an activity in an app without ever installing it, android instant apps is the right choice. If you're looking for a way to pass information "deeply" from the user's email or internet to your app, deep linking is advised.
Intent filters can be used to open some apps from web pages. When a link is selected that could be opened in a browser or in an app (or multiple apps), a box will pull up from the bottom of the phone's screen, asking for the user's preference for the default to open in on future clicks. (see image below) The web site's javascript may have to be altered to detect the user's operating system and send the proper URL call. iOS works a little differently.
Here is how to use an intent filter. It has some extra code that you may find useful..
public void startNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
// brings user to the market if app is not installed already
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // opens a new "page" instead of overlapping the same app
context.startActivity(intent);
}
If you need to pass data at the same time, use intent.putExtra("name", "value");

Run application in the background

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

Categories

Resources