Can I launch an activity from boot-up and have it go into the background without the user seeing it, android
At the moment I have code that starts an application from boot-up but opens it into the foreground. This was done by
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent start = new Intent(context, ApolloMobileActivity.class);
start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(start);
Then to get it sent to the background at boot-up I created another java file called, StartAtBootService so I changed the receiver class to:
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.ssab.StartAtBootService");
context.startService(i);
}
And the Service class was
public class StartAtBootService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "StartAtBootService Created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
/*
* In Androi开发者_开发问答d 2.0 and later, onStart() is depreciated. Use
* onStartCommand() instead, or compile against API Level 5 and
* use both.
* http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
@Override
public void onStart(Intent intent, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
}
*/
@Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
}
}
Is it possible to change the StartAtBootService to run an activity in another java file called ApolloMobileActivity in the background? I have tested this code and even though it runs in the background at boot-up it doesn't run the code in ApolloMobileActivity.
Please help! Thanks guys :)
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
from Activities
You can start an Activity, but there is no such thing as an invisible Activity. These are UI-components. If you want to do invisible work in the background, you have to do it in your service.
精彩评论