Android-Java: Starting an activity from a Broadcast Receiver - java

I'm trying to launch an activity directly from a broadcast receiver without the user having to manually open up my app. What I'm hoping to achieve is to automatically open up my app after a device restart. I have a receiver listening for a BOOT_COMPLETED event, which from that I want to open up an activity, or start my app.
Is this possible?
This is the code I'm currently using to launch the activity inside the receiver:
Intent i = new Intent();
i.setClassName(packageName, activityName);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Related

How to start an Android App by receiving a SMS?

I have written an Application, which checks when you receive a new SMS and if its from a certain number, the app plays the ringtone. Now, the App should check if there is a new SMS even if it isn´t lauchned, and so my question is: How can I launch my App by receiving a new SMS?
I tryed to upload my existing code but it didn´t worked, I am sorry.
To start an activity from almost everywhere you can simply call:
Intent myIntent = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(myIntent);
That should also work from your BroadcastReceiver which is triggered while receiving the sms.

Is there any way to prevent activity from closing while minimizing the app?

My main activity say A is open and by firing an alarm I am opening another activity say B using following code
final Intent intent = new Intent(context, BasicTemplateActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
when this BasicTemplateActivity is Foreground and I am minimizing the app this activity is dismissing. I want to make that activity as sticky. It should not close while minimizing.

Passing data from android activity to broadcast receiver on device boot up

I always get bundle value null when I pass data from activity to broadcast receiver.
My receiver will start on boot up.
This is the code in my activity class
Intent intent= new Intent();
intent.setAction("android.intent.action.BOOT_COMPLETED");
intent.putExtra("test", "test");
sendBroadcast(intent);
This is the code in my receiver class:
String testValue = intent.getStringExtra("test");
Your code in activity will never be called upon booting up. System invokes onReceive() with its own intent. You can check this by putting some logs in activity code - this log will not be printed in logcat.

Bring app in background to front

I am making an app that gives an alarm when you are close to a gps position. now when a user presses the home button this app get send to the back. When the users enters the region to trigger the alarm, the alarm triggers but the app isn't send to the foreground.
How can i do this?
I tried this
Intent intent = new Intent(context, TrainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
in the function of the alarm, but this does nothing? (Trainactivity is the class that runs on the background and the class that needs to be on the foreground)
The REORDER_TO_FRONT flag only controls the stacking order on the activity history. That is not what you want. Is this what you are looking for ? - Bring application to front after user clicks on home button

How to remove all activity in flow from stack, when calling some new activity in android

I have a problem in my android app, it having the log out functionality in setting screen.
When we Logout it opens the login screen. But when i press back button then it show the setting screen page, which is not required (as it takes me to inside the app without login). I am using the following code but it is not working. Because at the time of logout LoginActiviy is not exist.
Intent intent= new Intent(HomeSetting.this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Please suggest me some solution, that LoginActivity get call as a new Activity, and all activity in history will get destroyed.
You can set noHistory property of Activity in manifest file as true.So it will be removed from Activity satck,when it goes to background.
To avoid this, you should set flags as follows:
Intent intent= new Intent(getApplicationContext() , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Categories