POC - Start Service on boot without an Activity - Android 4+ - android

This question may seem a trivial but I've been struggling with it.
I'm trying to start a service on boot and everything works fine if I start it at least once from the mainActivity (launch activity), something like:
AndroidManifest.xml
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<activity
android:name="com.example.mainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.bootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="com.example.someService" android:enabled="true" android:exported="false"/>
...
bootReceiver.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
mainActivity.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
With the above code I'm able to run the service after every boot without any problems.
As a POC, I'm trying to start a service without any activity, or at least without the mainActivity, just by declaring the service on the AndroidManifest.xml or by creating new activity (invisible?) that is launched at the same time as the default launcher activity.
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Is there any way to achieve this ?
Can I start 2 activities from the AndroiManifest.xml when the user launches the app?
I'm sorry if my question isn't 100% clear, I've tried to explain it the best I could, if you cannot understand it, please leave a comment bellow. Tks.

As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Android 3.1, actually, to help prevent drive-by malware.
Is there any way to achieve this ?
Something has to use an explicit Intent to invoke some form of IPC on one of your components, to move the app out of the so-called "stopped state" that is preventing you from receiving the broadcast. So, something needs to either:
start one of your activities via an explicit Intent, or
start one of your services via an explicit Intent, or
send a broadcast to one of your receivers via an explicit Intent
(I don't know if trying to connect to a ContentProvider will work, though arguably it should)
The key is the explicit Intent. That's why invoking an activity from a home screen launcher works, because the Intent used to start your activity will be an explicit one.
However, in the absence of such an activity, you would need to find something else that would use an explicit Intent to invoke one of your components. Certain specialized services (e.g., an input method) probably get invoked with an explicit Intent if and when the user activates that app's capability via the system Settings app. If you're a plugin for some other app, that other app might use an explicit Intent to work with one of your components. You can ask the user to install the Android SDK, learn how to use the command line, and invoke an adb shell am command to start one of your components. And that's about all I can think of off the top of my head. None are exactly general purpose solutions.
or by creating new activity (invisible?) that is launched at the same time as the default launcher activity
I have no idea what you think that would achieve. If the user starts up your launcher activity, you're already out of the stopped state and will receive broadcasts as normal.

Related

Android: Threat implicit intent in a class that is not an Activity or BroadcastReceiver

I want to start an app with an implicit intent, but before that app starts, I need to process the intent and start a state machine that will then load an Activity.
I can do this either using a broadcast receiver or an activity
<receiver android:name="com.13.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.13.StartStateMachine" />
</intent-filter>
</receiver>
But then I have no control if other app will respond to this same intent.
or
<activity android:name="com.13.MyReceiverActivity">
<intent-filter>
<action android:name="com.13.StartStateMachine" />
</intent-filter>
</activity >
But this would not be clean, I can do some processing onCreate() and then finish().
Any idea if I can have a combination of these methods?
I mean use an implicit (or explicit) intent to load a class (that is not a BroadcastReceiver or Activity) from another app?
Since both apps are yours, you are welcome to use whatever IPC mechanism that you want.
The advantage of using an Activity is that you have the option of saying everything is in the same task. So, when App A starts an activity of App B, that can all be one task. Any other IPC mechanism will force you to start a new task.
So, as I suggested in a comment, use PackageManager to convert your implicit Intent into an explicit one. Then, use whatever IPC that you want. If you want to use a "broadcast" (where the explicit Intent really makes it more of a "narrowcast"), you are welcome to do so.
IMHO, saying that an activity transfers control to another activity, perhaps using finish(), is not really "dirty". After all, many of the splash screens that apps use wind up doing just that. IMHO, using any other IPC mechanism to start an activity, when startActivity() would work just fine, is "dirty".

Start Android Service with no Activity using an intent

i created an android application with no activity. I want to start a service using a system intent like BOOT_COMPLETED. I use the following receiver:
<receiver android:name=".autostart" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
I got the problem, that the intent is not received when power is connected/disconnect or boot completed. Is an Application with no Activity even in stopped mode after install? How can I start the service? UI is not possible because the application has no activity...
Is an Application with no Activity even in stopped mode after install?
Yes.
UI is not possible because the applicatio has no activity...
Then add one. You need one anyway, to present your license agreement, your online help, your configuration for this background processing, and so forth. And, since your app will not run until the user launches this activity, you need to for that reason as well.
Every Android application needs to be launched at least once after installation and only then it will receive any intents from system. This means an application without any gui will not work in your case.
Many applications include only "about" activity, which is a common way to deal with that.
Please see:
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
http://developer.android.com/about/versions/android-3.1.html#launchcontrols

Broadcast receiver not receiving intent

I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:
Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"
Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);
I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:
<receiver
android:name=".receivers.ReportingReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.app.FILTER_INIT_REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Curious why the ReportingReceiver class is not getting the intent call?
If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.
Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.
See http://developer.android.com/about/versions/android-3.1.html#launchcontrols
As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.
Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.
http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES
You can read more about this Android 3.1 change here
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
and here
http://code.google.com/p/android/issues/detail?id=18225

Restarting an app via an external Broadcast Receiver

I have implemented an app that is basically a custom app store for updating and launching a family of related apps. It also needs to update itself, which works, but the app is killed without warning during the install process. I want to automatically restart the app in this case so that the user can continue to use it immediately after an update.
So I made a separate application including only a single Broadcast Receiver that listens for package events for the first app's package name and starts a new activity. That receiver is never called:
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<receiver android:name=".AppUpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
In searching for similar implementations I have seen directly contradictory information on whether an application with only a receiver will ever execute, and whether a receiver will be called if its app is not already running. I've even come across example code of an application containing only a receiver, with a manifest very similar to my own. So what do I need in this application to ensure that the receiver is called whenever another package is installed?
If there is a better solution, I'd be happy to hear it.
Depending on the version of Android, you might need to start an application component in order for the BroadcastReceiver to be registered. By this I mean there will need to be a launcher Activity which must be started manually by the user.
From Honeycomb (I think) onwards it isn't possible to have application components 'active' unless the app has been manually started in some way. The reasoning behind this is the potential for insecure code executing without the end-users' knowledge.
I suspect this is what you're experiencing. To test it, add a simple "Hello World" Activity to the app that has the BroadcastReceiver in it. Launch the Activity and then check to see if the BroadcastReceiver then gets called after your other package is updated.

How to solve foreclose problem in start up my application?

I am developing an android application,I want to my application automatically invoked,when I switch on my device.so i used this permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> in my application's manifest.xml file and i used service , activity and register Broadcast receiver in manifest.xml file.but I got foreclose error appear in when will i start up my device.How solve this proplem.
My Receiver code given bellow
context.startActivity(new Intent(context,ServicesDemo.class));
//In my mainfest.xml for myReceiver
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter> </receiver>
I got this exception java.lang.RuntimeException: Unable to start receiver com.servicedemo.MyReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I am developing an android application,I want to my application automatically invoked,when I switch on my device
Users generally hate that with the fiery passion of a thousand suns. Please make this configurable and disabled by default.
Also, bear in mind that if too many developers abuse this, the core Android team is likely to prevent you from starting an activity at boot time.
How solve this proplem
Do what the error message told you to do: add the FLAG_ACTIVITY_NEW_TASK flag to your Intent.

Categories

Resources