Open android application when device opens - android

I'm developing an android application that I want that starts when the device is opened and remains on foreground until it is closed. To achieve this I have used some tricks that I found surfing the Internet:
Start application when device starts >> Solved using a broadcast receiver that handles the android.intent.action.BOOT_COMPLETED
Ignore the Home button when the application is opened >> Solved using an Activity Alias that is installed when the app starts and uninstalled when the application closes
I test the solution and it works fine almost always. When I close the device with the app executing and it restarts the device the application is opened twice. I think that the extra opening is due because the Activity Alias wasn't uninstalled because the app doesn't close properly when I shutdown the device.
Is there a solution to avoid this behavior?
Thanks

You should use ACTION_SHUTDOWN and perform necessary task on shutdown.Find more details here.

You can maybe use the Shutdown intent to finish your activity when the device is shutting down.
<receiver android:name=".myReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
Then you can just use the activity.finish(); to close your app.

Related

Android how to start activity on Boot-up

Let me describe you what I want: I want to build a "master app" and set it so that when the phone is powered up, it immediately goes into the master app. The user can never exit this app (this will be used for something like parental control), and he can only launch other apps from within it.
Basically it will be like a "custom desktop".
I must stress out, it is important that this app never exits. As long as the phone is started, this is the only environment that the user has access to.
Now after I explained what I need, I will need your help to tell me what am I looking for. Is this some kind of "default launcher" that I keep hearing about? Or how is this called?
How can I do it?
Thanks
Add this into Manifest.
<receiver
android:name=".Bootupclass"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
</receiver>
Bootupclass
public class Bootupclass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//write intent here
}
}
I need someone to give me the exact code needed to make the app:
Here Goes,
If you make an app as a launcher app, and if it is the ONLY launcher app within your system, it will of course be started when you switch on your device. And also when you click on the home button, since your app is the only launcher app within the system, the same would be started.
so ,
1) start on bootup 2) be the default action when pressing the home button (the "desktop")
could both be merged into 1.
You mentioned that your phone is rooted, So easiest way to achieve what you require would be to
1.Install your app with just these lines within the manifest. i.e within your first activity. Nothing else is required. (this would make your app as a launcher app)
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
2.Un-install the default launcher application within your device.
What is being asked in the question body is different from the question title.
Listening to BOOT_COMPLETED will do literally what is requested by title - give control to the application once when device is powered on or restarted.
It is not enough to achieve the real goal - to prevent user from exiting the application and reaching "normal" home screen - once user presses "exit" or "home", he will essentially leave your app. While you can intercept "Exit" button and prevent it from quitting your app, "home" button is not possible to block programmatically.
In order to make and app like parental control and prevent user from reaching unwanted apps you need to implement "custom home screen" or "custom launcher" (which is the same thing).
It is rather large topic, but this seems to be a good starting point: Android - creating custom launcher.
And because author insists on "exact code", to make your app to start instead of a home screen (after boot or pressing "home" button), you need the following in the manifest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
After install, press "home" button - you will be given a choice of standard launcher or your app. Check "use as default" and select your app - from now on it will start instead of the normal home screen.
Beware, though, that there are few known issues with custom launchers. One is - you have to block access to settings, otherwise user can switch back to default launcher. Also, after your app is updated (i.e. you post new version) user will be asked what home screen to use and can choose default launcher.
I must stress out, it is important that this app never exits This I believe is not possible.
From your requirement it seems that you require a MDM solution. Upwards of Android 2.2 device administration APIs have been available. Check out http://developer.android.com/guide/topics/admin/device-admin.html
The user will have to install your app and allow it to be a Device Administrator. (You can see currently available administrators from Setting -> Location and Security -> Device Administrator). Now as an administrator you can control features on device eg. disallow apps to be uninstalled, prevent installation of specific apps, disallow launch of specific apps. wipe device if security is breached etc. You can also prevent your app from being uninstalled.
There are apps available with such features. I can name Maas360 off the top of my head.
Disclaimer: I haven't tried the device administration apis myself.

Have Android app auto start on reboot [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android, how to determine if a reboot occurred?
I have an app that I would like to always be running on my phone.
Is it possible to have an Android app programmatically start after the phone has been power cycled?
Add permission android.permission.RECEIVE_BOOT_COMPLETED to your manifest. Write a broadcast receiver and add this filter to the manifest:
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Have your receiver launch any service or perform any task it chooses.
I don't think it's a good idea to try to launch an activity at boot time, but maybe it would work out ok.
It's an OK idea to have an app start after a reboot. It's not OK to put an Activity into the foreground after a reboot. Activities should always be under user control. In general, try not to out-guess the users.
However, you may have an interesting use case, so please post it.

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.

android : detect the reinstall of an app from the app code

Is there a way to detect from app code when it is being reinstalled.
I saw that app update from market can be detected by listening to PACKAGE_REPLACED event in broadcast receiver. But that these events are not delivered to the app , if the app is reinstalled from editor (eclipse ).
My requirement is that i am disabling a component(Launcher activity). The app install will fail if it is not enabled. SO everytime before reinstall i want to enable this component.
I am talking about the reinstall before publishing in market. While developing , each time i reinstall the app to test some modification, i want to detect this from my app and make the component enabled.
I saw from my example that the following events will be broadcasted when an app is reinstalled from eclipse.
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
</intent-filter>
I defined a broadcast receiver for these actions and from onreceive i am able to enable my component

Android - [Xoom/Honeycomb] application without LAUNCHER activity does not work

I have an application without launcher activity that works properly from Android 1.5 to Android 2.3.4. It is started by my broadcast receiver. However, on Honeycomb (Motorola Xoom), my broadcast receiver doesn't work at all (it does not catch any intents). If I add launcher activity to my manifest:
<activity android:label="#string/app_name" android:name="com.myapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
Then my app works properly (broadcast receiver catches all necessary intents and starts my services).
I would be very grateful for help!
Are you running Honeycomb 3.1 or above? If yes take a look here.
When your application is installed, it is in stopped state. When the application is first launched, it is moved out of stopped state.
A application in stopped state won't get started by all broadcast intents. The sender of the broadcast intent has to specify the Intent.FLAG_INCLUDE_STOPPED_PACKAGES flag if it wants to launch stopped applications too.
Intent intent = new Intent(MY_INTENT_ACTION);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
If you can't change the code which sends the intent, your best bet would be to keep the launcher activity. Whenever the user launches your application after installation, it will be moved out of the stopped state and you will start receiving broadcasts.
Note the the user can move your application back to the stopped state from Manage Applications in device settings.
On Android 3.1 and higher, BroadcastReceivers will not be invoked until the user has started the application at least once by some other means, such as launching an activity.
Since users tend to get very confused by applications that do not have an activity, you really should have had an activity in the first place, for online help if nothing else. With Android 3.1, that is even more important.

Categories

Resources