Starting my activity when power button has been pressed - android

I am trying to develop an app which starts flashlight when user presses power button.
In that process I got to know how to listen to power button by catching Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON intents.
Now I am trying to start my activity after receiving these intents.
I am using this code in my receiver to catch intents
public class IReciever extends BroadcastReceiver{
private Activity acti = null;
public IReciever(Activity act) {
acti = act;
}
#Override
public void onReceive(Context ctxt, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Intent i = new Intent(acti, MainActivity.class);
acti.startActivity(i);
Toast.makeText(ctxt, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_SHORT).show();
Log.e("In on receive", "In Method: ACTION_SCREEN_ON");
}
}
}
Here is the code of my MainActivity
public class MainActivity extends Activity {
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
IReciever mReceiver = new IReciever(this);
registerReceiver(mReceiver, filter);
}
}
The app shows toasts and I am able to see logs which confirms that I am able to receive intents.
But I doesn't start my MainActivity. Please help me

Register your receiver in manifest not in the activity.
and use below code to start your activity
Intent in = new Intent(context, MainActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
in.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(in);
write this code in onreceiver..where you checked for power on and off.

Related

Can a Broadcast Receiver send information to an Activity only if it is active without starting a new Activity?

I have a broadcast receiver that gets triggered on geofencing events and either "clocks in" or "clocks out" with the server. If my application's "Attendance" activity is already open I would like it to display the clocking status change but I don't want the Broadcast Receiver to start the activity if it's not open - in other words display the change "live" while the activity is open only.
The way I imagine doing this is with the Broadcast Receiver sending an Intent to the activity but name "startActivity()" doesn't sound encouraging unless there are any special flags I can pass to prevent starting an Activity that isn't already open - I can't seem to find any.
The other option would be to constantly poll the value while the activity is open but it doesn't seem optimal so I would only use it if there wasn't another way and I can't think of a reason why it couldn't be possible with Intents.
There are several different ways to accomplish the same task. One is registering a listener like the following example:
MainActivity
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
public void onReceive(Context Context, Intent intent)
{
//Do something.
}
});
}
#Override
protected void onDestroy()
{
super.onDestroy();
Receiver.setOnReceiveListener(null);
}
}
Receiver
public class Receiver extends BroadcastReceiver
{
private static OnReceiveListener static_listener;
public static abstract interface OnReceiveListener
{
public void onReceive(Context context, Intent intent);
}
public static void setOnReceiveListener(OnReceiveListener listener)
{
static_listener = listener;
}
#Override
public final void onReceive(Context context, Intent intent)
{
if(static_listener != null) {
static_listener.onReceive(context, intent);
}
}
}
Just have your BroadcastReceiver send a broadcast Intent. Your Activity should register a listener from this broadcast Intent and if it gets triggered, it can update the UI.
Here's an example:
Declare a private member variable in your Activity:
private BroadcastReceiver receiver;
In Activity.onCreate(), register the BroadcastReceiver:
IntentFilter filter = new IntentFilter();
filter.addAction("my.package.name.CLOCK_STATUS_CHANGE");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here you can update the UI ...
}
};
registerReceiver(receiver, filter);
And in onDestroy() you can unregister it (probably not necessary, but cleaner):
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
In your BroadcastReceiver that detects the geofencing event, you should create and send a broadcast Intent:
Intent broadcastIntent = new Intent("my.package.name.CLOCK_STATUS_CHANGE");
sendBroadcast(broadcastIntent);

Start Activity When Foreground Service Stop

I am trying to open new activity and pass some data from service to activity when the foreground service stops even if the app is killed.
My service class tried this thing in onDestroy
#Override
public void onDestroy() {
Intent intent = new Intent("UsedTime");
intent.putExtra("Status", "1500");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Intent i = new Intent(this, UsedTimeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_MAIN);
startActivity(i);
}
and here is My UsedTimeActivity
public class UsedTimeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vpnused_time);
LocalBroadcastManager.getInstance(context).registerReceiver(
mMessageReceiver, new IntentFilter("UsedTime"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("Status");
Log.i("TAGUSEDTIME", "onReceive: " + message);
}
};
}
It doesn't work when the app is killed from the background and works fine when the user interacting with the user
Tested Device - Redmi9A (Android 10) & RedmiNote10Pro (Android 12)

Android fragment activity not receiving ACTION_SCREEN_OFF or ACTION_SCREEN_ON even

I think I may be missing some little detail and will feel very stupid once this is solved but....
I have many activities that all extend a base class. In the base class there is a Broadcast receiver to catch ACTION_SCREEN_OFF and ACTION_SCREEN_ON.
This will log out the user and return to the main screen.
public class ScreenBReceiver extends BroadcastReceiver
{
public void autoLogout(Context context)
{
MainActivity ma = new MainActivity();
ma.setLoggedIn(false);
Intent homeIntent = new Intent(context, MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
FDFragmentActivity.this.finish();
context.startActivity(homeIntent);
}
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_SCREEN_ON));
{
this.autoLogout(context);
}
}
}
public ScreenBReceiver screenLogout;
However, there is one (1) activity that is not receiving the action.
I cannot for the life of me figure out why the one activity is not receiving this action.
I don't know what else to post. If more info is needed please say so.
All of the activities in the manifest are declared identical.
Neither the xml layout nor the activity code specify that the screen be kept on.
What else could there be?
EDIT:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this;
mHandler = new Handler();
pi = new PollInterface(mContext);
screenLogout = new ScreenBReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
IntentFilter filter2 = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(screenLogout, filter);
registerReceiver(screenLogout, filter2);
.......
#Override
public void onDestroy()
{
super.onDestroy();
try
{
unregisterReceiver(screenLogout);
}
catch (IllegalArgumentException iae)
{
// Android can't just let it pass that a receiver was already unregistered.
// So, just ignore the daft exception it throws when unregistering twice.
}
}

android recognize if call has been answered

I'm trying to add to my broadcast receiver an if, so I will start a different activity if a call has been answered, to the regular activity I start usually, if the screen has just been turned on.
Now as you can see down here I have the class screenJump starting when the user wakes up phone.
I would like to start a phoneActivity I wrote when the user wakes up phone, but only when a call has been answered.
This is my service now.
public class MyService extends Service {
BroadcastReceiver bd;
public MyService() {
}
class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startupIntent = new Intent(context, ScreenJump.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupIntent);
}
public ScreenReceiver()
{
}
}
#Override
public void onCreate()
{
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
bd = new ScreenReceiver();
registerReceiver(bd, filter);
}
}

How to bring application from background to foreground via BroadcastReceiver

I have two classes which are MainActivity and MyBroadcastReceiver. BroadcastReceiver detects whether phone screen is on or off. My desire is to launch my application whenever screen lock is released. I mean that I want to bring my application to the front when phone lock releases.
Here is my activity class:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver();
}
private void registerReceiver(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new MyPhoneReceiver();
registerReceiver(mReceiver, filter);
}
}
And here is my broadcast receiver:
public class MyPhoneReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()){
//Bring application front
}
}
}
What am I supposed to do in order to perform this operation in my broadcast receiver?
Try to use FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_SINGLE_TOP
Do the following in your onReceive method of the BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent();
newIntent.setClassName("com.your.package", "com.your.package.MainActivity");
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
You need the flag "FLAG_ACTIVITY_NEW_TASK" for your intent otherwise there will be a fatal exception being thrown.
The flag "FLAG_ACTIVITY_SINGLE_TOP" is to bring your MainActivity to the front and you can proceed to do whatever you want from there by overriding the onNewIntent method in MainActivity.
#Override
protected void onNewIntent(Intent intent) {
// continue with your work here
}
Do this inside your onReceive method:
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
You might need to tweak the addFlags call for your specific needs!

Categories

Resources