Launch Android Application Without Main Activity and Start Service on Launching Application

Launch Android application without main Activity and start Service on launching application

You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:

  1. In your Manifest, set the Activity theme to Theme.Translucent.NoTitleBar.
  2. Don't bother with a layout for your Activity, and don't call setContentView().
  3. In your Activity's onCreate(), start your Service with startService().
  4. Exit the Activity with finish() once you've started the Service.

In other words, your Activity doesn't have to be visible; it can simply make sure your Service is running and then exit, which sounds like what you want.

I would highly recommend showing at least a Toast notification indicating to the user that you are launching the Service, or that it is already running. It is very bad user experience to have a launcher icon that appears to do nothing when you press it.

Android application as a service without activity

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to.

The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher. If there's no activity, you'll have to find another way to start it.

It is easy to do, though. Just fire an intent from one of your other programs, like this:

startService(new Intent("my.service.intent"));

... where the service is registered your manifest, like this:

        <service android:name=".SomeService" >
<intent-filter>
<action android:name="my.service.intent"/>
</intent-filter>

You could use that intent to pass Parcelable parameters to the service, and the service can reply by broadcasting intents back.

Of course startService and broadcastIntent are a bit clunky if you really need a complex API between applications and your service. If you need something richer, you will want to look into AIDL and a Bound Service.

Edited to add Intent Filter

How do I create an application without a main activity in android studio?

It's perfectly fine to have no activities in your application. The build succeeds, Android Studio only fails when it tries to run it, because it doesn't know what to do.

You need to modify your run configuration. Go to "Run > Edit configurations" and under "Launch options" change "Default Activity" to "Nothing":

Sample Image

Start Service at boot without activity

From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

Therefore in your application you must have one Activity to run BroadcastReceiver.

Starting a non-Main Activity from other app clearing its top

After a lot of research and tries, I've found that adding the next parameter to the activity in the manifest works like a charm. I hope this can help someone else:

android:launchMode="singleInstance"

How to Start android service without activity

How to do it ?

By using Context.startService() or Context.bindService() methods.

where Context might be Activity, Application, argument of the onReceive() method of BroadcastReceiver etc.

Android app without an activity which will act as service and start on device bootup?

Is it possible to make an app without an launcher icon even in android lollipop?

Not realistically. for apps to be distributed through typical channels (e.g., Play Store). You will need a UI to:

  • Show the license agreement
  • Provide access to documentation and support
  • Request Android 6.0 runtime permissions, such as working with SMS
  • Allow any of the rest of your code to ever run

Noteworthy exceptions:

  • Apps that are shipped pre-installed on a device or custom ROM
  • Apps that are purely plugins to some other app

And is it possible to start service on bootup?

Yes... once the user has clicked on your launcher activity, or something else uses an explicit Intent to start up one of your components. If you lack a launcher activity, and you are not a pre-installed app, and you are not a plugin for another app, then your BOOT_COMPLETED BroadcastReceiver — which you would use to start your service — will never be invoked.

And one more thing that service is going to have access to sms of device and will trigger some action if there is some special predefined keyword in sms, is it possible?

Yes... but you will need a launcher activity to be able to request the required runtime permissions from the user, on Android 6.0 and higher.

start activity without launching the application

startactivity(intent) pops up the backstack activity or creates a new one if it doesn't exist.. so your solution is there is onResume() and onPause().. onPause() is called when you the activity is gone, onResume() is called when you see the activity, so my advice to you is create a boolean, it can be in a singleton class

public class MySingletonClass {
public static boolean startloginpage; // by default its false
}

then you in mainactivity or the activity that the user will launch or come back to, put the code in its onresume

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(MySingletonClass.startloginpage){ //check if your boolean is true
// if it checks out then call do what you want to do when the user times run out
}else{
// if it doesn't check out continue without telling user to login,
}
}

in your service remove the intent code and put this

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
intent.putExtra(HAS_SIGNED_OUT, true);
MySingletonClass.startloginpage = true;
return START_NOT_STICKY;
}


Related Topics



Leave a reply



Submit