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