Autostart phonegap on android

1,317 views
Skip to first unread message

thisk

unread,
Feb 25, 2010, 4:09:16 PM2/25/10
to phonegap
Hi Guys!

In one of my projects the client wanted to auto start phonegap on
android devices. The solution is really only a few lines of code and
might help others:

In AndroidManifest.xml (in project root):

Add <uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> just after
the already existing permissions.

Add

<receiver android:enabled="true" android:name=".DroidGapReceiver"

android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /
>
<category android:name="android.intent.category.DEFAULT" /
>
</intent-filter>
</receiver>

inside the <application> tag, just before the first <activity> tag.

In the src folder of the project, create a class called
DroidGapReceiver with the following content:

package com.phonegap.demo;
import android.content.*;

public class DroidGapReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent droidGapIntent = new Intent(context,
DroidGap.class);
droidGapIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(droidGapIntent);
}
}

That's it. Of course the package name and the name of the receiver
class name can be changed.

Regards,
Keve

Keve Kutner

unread,
Apr 26, 2013, 1:37:17 AM4/26/13
to phonegap

Thanks Ryan!

My stuff is 3 years old so it needed an update :)

On Apr 26, 2013 2:46 AM, "Ryan Wheale" <ryan....@gmail.com> wrote:
You are almost there.  I had the same issue.  The code provided by @thisk is very helpful, but not quite correct.

In his code, he is starting the DroidGap.class on startup, but per the comment in the actual DroidGap class file:

 * This class is the main Android activity that represents the PhoneGap
 * application.  It should be extended by the user to load the specific
 * html file that contains the application.

... so, instead of loading the DroidGap.class, you need to load the YourApp.class (which extends DroidGap).  The easiest way to find this is to look at the top of you AndroidManifest.xml file and find the "package" name:

<manifest xmlns:android="..." package="com.package.YourApp" ... >

Now that you know your package name, go back to your custom class file that you created (see instructions above) and replace the following line:

import android.content.*;
import com.package.YourApp;

And then change the following line:

Intent droidGapIntent = new Intent(context, DroidGap.class);

Intent droidGapIntent = new Intent(context, YourApp.class);

That should do it.  Cheers, and thanks @thisk for the foundation.

~Ryan Wheale


On Saturday, March 2, 2013 10:11:58 AM UTC-7, Daniel Beauyat wrote:
Hi!

This code des not work on any phonegap I try on.
I use phonegap 2.5.0 and this only lead to FC upon startup.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
 
To compile in the cloud, check out build.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ryan Wheale

unread,
May 3, 2013, 8:10:51 PM5/3/13
to phon...@googlegroups.com
My previous comment is a little incorrect.  I have created an AutoStart plugin which comes loaded with "enable", "disable", and "isEnabled" methods.  Works like a charm and is app-agnostic - meaning it automatically detects the startup package name for your app and registers it as a boot task.  I will be posting it to Github soon and will reply back here with a link.

~Ryan

Jeff Greer

unread,
Nov 13, 2013, 7:04:03 PM11/13/13
to phon...@googlegroups.com
Hi Ryan,

Did you ever make your plugin available?

Thanks

Jeff
Reply all
Reply to author
Forward
0 new messages