autostart activity -> problem: app starts twice

Put your problem here if it does not fit any of the other categories.

autostart activity -> problem: app starts twice

Postby Chri » Mon Feb 13, 2012 2:42 pm

I would like to run my application when the boot up of my device has finished.
For this I have found some helpful code in the internet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public class MyBootCompleteReceiver extends BroadcastReceiver
  2.     {
  3.  
  4.             @Override
  5.             public void onReceive(Context context, Intent intent)
  6.             {
  7.                     if(intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
  8.                     {  
  9.                            Intent pushIntent = new Intent(context, MainActivity.class);  
  10.                            pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  11.                            
  12.                            context.startActivity(pushIntent);  
  13.                     }
  14.             }
  15.     }
  16.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

And the Manifest entry:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         <receiver android:name="MyBootCompleteReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
  2.             <intent-filter>
  3.                 <action android:name="android.intent.action.BOOT_COMPLETED"/>
  4.                 <category android:name="android.intent.category.DEFAULT" />          
  5.             </intent-filter>      
  6.         </receiver>
  7.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Now if I boot up my device the application seems to start up twice.
Then I tried to replace the "NEW_TASK" Flag with the "REORDER_TO_FRONT" (I have also read this in the internet) but if i do so, my application crashes on boot up.
Also if I remove all flags, it crashes too.

Hope you can help!
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Top

Re: autostart activity -> problem: app starts twice

Postby Phyll » Mon Feb 13, 2012 11:03 pm

Hi Chri,

I think you should make the receiver something like this:

Code: Select all
      <receiver

            android:name="com.your.package.MyBootCompleteReceiver" >

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
      </receiver>


And that permission for boot completed belongs outside the application tags but inside the manifest tags. Not in within the receiver tags.

Then, in your receiver I think you will have to find the flag that will restart the app if its already running instead of starting a new one. Sounds like something like that is happening.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: autostart activity -> problem: app starts twice

Postby Chri » Tue Feb 14, 2012 12:55 pm

Thank you phyll for your answer,

I have changed the manifest like you described and tested near all flags (also some one in combination).

The sad, sad result is that if I set no flag or any other flag, the app crashes only if i set the FLAG_ACTIVITY_NEW_TASK the app doesn´t crash but it starts twice.

Any other ideas, how it could work?

wkr
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Re: autostart activity -> problem: app starts twice

Postby Phyll » Tue Feb 14, 2012 10:56 pm

Hi Chri,

I added your code the way I think it should be to an example app I had and it works fine I think. When the emulator boots it brings up the activity. Is that what you want to do?

Here is what the receiver looks like. Its sending an intent to start the app (App2) yours is different.

Code: Select all
public class MyBootCompleteReceiver extends BroadcastReceiver
{

        @Override
        public void onReceive(Context context, Intent intent)
        {
                if(intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
                { 
                       Intent pushIntent = new Intent(context, App2.class); 
                       pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                       
                       context.startActivity(pushIntent); 
                }
        }
}


Here is what the manifest looks like for App2 with your receiver in it:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.bandpsoftware.app2"
       android:versionCode="1"
       android:versionName="1.0">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />     
       
   <application
      android:icon="@drawable/and_icon"
      android:label="@string/app_name">
     
     

      <!-- Icon Copied-C:\Program Files\Borland\Delphi7\AndroidStuff\and_icon.png C:\Program Files\Borland\Delphi7\AndroidStuff\App2\res\drawable\and_icon.png -->

      <activity android:name=".App2"
         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="MyBootCompleteReceiver"
             android:enabled="true"
             android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT" />         
            </intent-filter>     
        </receiver>
     
    </application>



</manifest>


No running twice that I could tell. I stuck a Log statement in the oncreate method of App2 to check that and it showed up once in the logcat.

Hope this helps.

Phyll
Last edited by Phyll on Thu Feb 16, 2012 10:23 pm, edited 1 time in total.
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: autostart activity -> problem: app starts twice

Postby Chri » Wed Feb 15, 2012 1:03 pm

Hey Phyll,

I tried your source code out, but unfotunatelly it doesn´t work on my real device (Asus eee pad transformer with android 3.2.1)
Then I also tried my application in the emulator (Version 3.2) and the autostart function worked quiete fine.
The app started after boot up once and there were no errors.

So the problem should be the real tablet.
No idea what I can do in this case.

Thanks for your help.
wkr Chri
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Re: autostart activity -> problem: app starts twice

Postby Phyll » Wed Feb 15, 2012 1:18 pm

Hi Chri,

Well at least you know its not all in your code. I'm sorry I can't help you sleuth that too much because I don't have access to one of those devices.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: autostart activity -> problem: app starts twice

Postby Chri » Wed Feb 15, 2012 1:22 pm

No matter,

you have done enough :)
Maybe the problem will bey solved by an update (probably to 4.0)
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 8 guests