0

I have a service which will run in background 24/7. I've used BroadcastReceiver to restart/awake the activity in certain situations like phone reboot,connected to internet,time changed etc.

But,this resulting to the situation where activity starts and app open up again and again.

So,I thought of a solution where whenever receiver is invoked,after starting the activity show the home screen instead of app.

Manifest:

 <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <data android:scheme="package"/>
                <category android:name="android.intent.category.HOME" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE"/>
                <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"/>
                <action android:name="android.intent.action.DATE_CHANGED"/>
                <action android:name="android.intent.action.TIME_SET"/>
                <data android:scheme="package"/>
            </intent-filter> 
        </receiver>

Receiver:

@Override
    public void onReceive(Context context, Intent intent)
    {
           intents.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intents);

        Intent i = new Intent(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
      //  startActivity(i);  //this will not work.
        context.startActivity(i);  //so tried this but failed
    }

Nothing is working.

Please,I don't wanna use alarmmanager...

Any suggestions would be of great help. Thank you.

Clarification::

I have to start activity then show home page,once I start my activity all the related functions will be started again..

I want something like this:

context.startActivity(MainActivity);  start main activity
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);   //show home page of device..

But this will not happen as this is a receiver class but not activity. then what is the alternative for this situation

Prabs
  • 4,923
  • 6
  • 38
  • 59
  • You've coded your receiver to start your main activity, so why are you surprised that your App opens again and again? What do you want to happen? – adelphus Aug 07 '15 at 12:19
  • @adelphus I have to start the activity but I don't want to disturb the user by showing the app.So,it should start then display home page instantly so that user not intimated about this. – Prabs Aug 07 '15 at 12:21
  • Then you're not understanding how Android works: Activities are UI-focused tasks, your App will always run when your Activity is started. You should look into creating a *service* which can run in the background without any UI. http://developer.android.com/guide/components/services.html – adelphus Aug 07 '15 at 12:23
  • Here's a recipe for you: http://stackoverflow.com/questions/9092134/broadcast-receiver-within-a-service – pelya Aug 07 '15 at 12:31
  • I have a service which implements `LocationListener` I've tried starting service from `onReceive` but in activity I have few tasks such as checking internet,updating db values etc.So I **have** to start activity. So isn't it possible to use category home in receiver class – Prabs Aug 07 '15 at 12:32
  • @Prabs *So I **have** to start activity.* No you don't. There is no reason to start an Activity if you are not showing a UI. Everything else can be moved into a service - which is designed for background actions. We've tried to help you, but you're refusing to accept the answer. If you run an Activity, *your App will always start* causing a distraction to the user. – adelphus Aug 11 '15 at 10:12
  • @adelphus I've ended up by starting the service instead of activity.I've checked for required connections in service..giving alert in service is a huge headache,so I've given notification if anything is missing..Finally done with the task.. – Prabs Aug 14 '15 at 05:25

0 Answers0