Android Service application - android

I'm writing a software suite for Android devices that will keep track of call times and various other call info. I'd like one of the applications to run as a Service without being started from an Activity, and I'd like it to initialize immediately when the phone boots, and stay running constantly, listening for phone calls, logging information to a database as necessary. The Service will need to present a dialog to the user at the end of each call.
2 questions:
How do I get the program (Service) to initialize at boot automatically with no user setting or interaction required?
I was under the impression that you cannot instantiate and display dialogs without using an Activity. I would like the dialogs to appear laid over whatever the user currently has on the screen, and display a dialog. Is there a way to make an Activity completely transparent over the current Activity or is there a way to display dialogs from a Service?
thanks in advance.

How do I get the program (Service) to initialize at boot automatically with no user setting or interaction required?
Add this permission to your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then add a receiver to your manifest:
<receiver android:name="your.package.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Create a receiver in Java code:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// start the service from here
}
}
I was under the impression that you cannot instantiate and display dialogs without using an Activity. I would like the dialogs to appear laid over whatever the user currently has on the screen, and display a dialog. Is there a way to make an Activity completely transparent over the current Activity or is there a way to display dialogs from a Service?
Yes, that's true. You have to have an activity that pops up the dialog. To create a transparent activity add the following style in your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#00000000</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
Then apply the style to your activity, for example:
<activity android:name=".TransparentActivity" android:theme="#style/Theme.Transparent">
...
</activity>

Related

Issue with Application Packaging in Kotlin

I have an app I built, deployed and is being tested with Google Developer Tester Option. My app(s) i have been developing has a wierd issue I can not put my finger on.
The App is a simple Note Taking App. So there is not a lot of code. I done my main activity only has about 60 lines of code. Everything else is composed of classes.
When I test my app on my cellphone. There is a time delay of 5 seconds that a white screen shows while launching. Then it goes to the app.
I would usually put a splash screen. And its seamless in loading. Without the splash screen with normal code it shows the white screen for 5 seconds.
My question is, am I doing something or wrong or is this something with the transition of .apk to .aab when packaging?
I have read articles of people saying its a bug withing Android itself. I have no warnings or errors within the app.
This has me puzzled and any advice would be helpful.
Here is a screenshot of the start up process.
Then this happens for 5 seconds then the main app shows
Any help is appreciated.
Ok after some test coding I finally solved the issue. To have the White Screen not appear you can create a splash screen or just a theme.
Theme should be set as follows
<style name="Theme.SplashScreen" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowDisablePreview">true</item>
</style>
Then in your SplashScreen.kt you can add the following
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}, 2000)
Then in your Manifest you need add the following:
<activity
android:name=".SplashScreen"
android:exported="true"
android:theme="#style/Theme.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
This cuts the launch time with the Splash Screen to whatever your delay is set at.
Your application can't launch instantly, the time it takes to launch an application depends on several hardware factors. So the launch time is different from one smartphone to another
https://developer.android.com/topic/performance/vitals/launch-time

Why the flash screen in many Android apps is not closed when the back button is clicked?

For example, when you open WhatsApp, during the launch of the app, the splash activity is on the screen. If the user changed his mind and preferred to close the app, the app does not close! He must wait till the app launch and the splash activity disappear … This is almost the case in all Google's Android apps that I have used … How to implement this behavior?
because they overried the onBackPressed() method (in splash screen activity)
override fun onBackPressed() {
//super.onBackPressed()
}
and comment the super.onBackPressed()
you can add logical condition there to decide wheatear to go back or not
override fun onBackPressed() {
if(condtion is true)
super.onBackPressed()
else
Log.e("","process pending")
}
if u MainActivity takes 3 sec to load launch SplachScreenActivity.class
and block it for 3 sec using thread or timer once timer expiries call finish() method which will get back to MainActivity where u find MainActivty ready
They're using a trick. The splash screen is actually just a theme on a dummy activity, which lets them render a static background drawable while the application is still being created by the system. This means you can't actually have any logic running, nor can you create any views on a splash screen, it is just a static image.
res/values/themes.xml
I believe android:windowBackground is the only attribute that will be accepted for a splash screen.
<resources>
<style name="AppTheme_Splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
</resources>
res/drawable/splash_screen.xml
In my example, I display two .png images on the splash screen.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/splash_screen_background" />
</shape>
</item>
<item>
<bitmap
android:antialias="true"
android:gravity="center"
android:src="#drawable/splash_logo"
android:tileMode="disabled" />
</item>
<item>
<bitmap
android:antialias="true"
android:gravity="bottom|center_horizontal"
android:src="#drawable/splash_logo_title"
android:tileMode="disabled" />
</item>
</layer-list>
AndroidManifest.xml
Apply the theme #style/AppTheme_Splash, and add MAIN and LAUNCHER intent-filters.
<manifest>
<application
android:name=".BaseApplication"
android:icon="#drawable/app_icon"
android:label="#string/launcher_name"
android:theme="#style/AppTheme_Light">
<activity
android:name=".ui.SplashActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme_Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.HomeActivity"
android:label="#string/app_name"
android:launchMode="singleTask" />
</application>
</manifest>
SplashActivity.java
The onCreate method will be executed after the application is fully loaded. No logic is executed until that point, nor would the activity's View be rendered until after the app is loaded.
public class SplashActivity extends AppCompatActivity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent homeIntent = new Intent(this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
}
I think that's it!

Standalone transparent activity is showing in recents holding previous intent

I have a StandaloneActivity which handles action.SEND which does some work and finishes.
<activity
android:name=".StandaloneActivity"
android:launchMode="singleInstance"
android:theme="#style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
It has transparent theme:
<style name="Theme.Transparent" parent="Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
StandaloneActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleIntentExtras(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntentExtras(intent)
}
private fun handleIntentExtras(intent: Intent) {
// do some work and set extras to intent
//Calling setResult() as I start this activity from another activity with startActivityForResult() for another usecase.
setResult(Activity.RESULT_OK, intent)
finish()
}
Expected is, whenever a user shares something with my app from another app, user should not leave current app but StandaloneActivity should be able to do work and finish. StandaloneActivity (my app) should not remain in recents.
The weird this is above setup works as expected in Java version of app but with Kotlin (migrating from Java to Kotlin) StandaloneActivity (my app) is visible in recents and on opening it, it is processing the previous intent and getting finished. StandaloneActivity remains in the recents.
I have tried to recreate the same setup with a simple demo app and after playing around with diffrent launch modes and flags I got the solution.
I have to exclude this activity from the recents, so I added following to the StandaloneActivity in the manifest along with existing setup-
android:excludeFromRecents="true"

Open invisible activity on share action (ACTION_SEND)

There are a lot of similar questions on StackOverfow but they don't solve my problem.
The task is to process in Service intents sent with default share mechanism (ACTION_SEND) from different applications.
But as far as ACTION_SEND is an activity action we have to pick up intent in activity and broadcast it to service. And it will be perfect if this activity will be invisible to the user.
I've researched a lot and have tried many solutions already.
First step of making activity invisible belongs to activity style.
I've tried this values.
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:theme="#android:style/Theme.Translucent"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
Also i"ve tried to create a custom theme.
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
How the activity in AndroidManifest.xml looks like.
<activity
android:name="xxx.xxx.activities.HandleShareIntentActivity"
android:theme="#style/Theme.Transparent"
android:noHistory="true"
android:excludeFromRecents="true"
android:label="#string/title_activity_handle_share_intent">
<intent-filter>
<action android:name="com.google.android.gm.action.AUTO_SEND"/>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.google.android.voicesearch.SELF_NOTE"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
Activity code. No setContentView(). finish() in onCreate().
public class HandleShareIntentActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, xxxService.class).putExtra(
Constants.ACTION_SHARE_KEY, getIntent()));
finish();
}
}
On Nexus 5, android 4.4.4 I do have black screen blink while handling any share event.
Any solutions?
I've tried this values.
Use Theme.NoDisplay.
I Just did this, and it helped in my case:
<activity
android:name=".SensorBoardActivity"
android:theme="#android:style/Theme.NoDisplay"
android:excludeFromRecents="true"
android:noHistory="true">
I see you already have finish(); but just for others it is important to execute it just before leaving the onCreate().

Pop up dialog in Android home screen

Is it possible to show pop up dialog (AlertDialog) in home screen on the android device via services?
You could create an Activity with the Theme.Dialog theme. In your AndroidManifest.xml file add the theme to the activity, like this:
<activity android:name=".DialogActivity" android:theme="#android:style/Theme.Dialog"></activity>
From your service simply start this Activity. You will have to start the activity with the Intent.FLAG_ACTIVITY_NEW_TASK flag. See How to start an Activity from a Service
Does anyone needs option, "android:launchMode="singleInstance", when pop activity in the broadcast receiver or Service??
Without this option, my app started automatically and pop MyDialogActivity above it.
And then, something happened wrong. (My app has the Main Activity with auto-login function. When the other new Activity started automatically, MyDialogActivity is hided by it.)
So, this is my sample xml code.
<activity
android:name=".MyDialogActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Dialog" />
<activity
I hope someone needs my comments. :)
You can not create dialog from service but we have one alternative solution is that You can create dialog activity and start that activity from your service
You can set Theme of activity as dialog by below way
<activity android:name=".MyDialogActivity" android:theme="#android:style/Theme.Dialog"
android:label="#string/app_name">
</activity>
Create CustomDialog As Activity
add to manifest
<activity android:name=".view.activity.CustomDialog"
android:launchMode="singleInstance"
android:screenOrientation="fullSensor"
android:theme="#style/AlertDialogTheme"
/>
add Style AlertDialogTheme to style.xml
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert.Bridge">
</style>

Categories

Resources