startActivity() in broadcast receiver bag(?) - java

I'm trying to start MainActivity with BluetoothDevice.ACTION_ACL_CONNECTED receive just like in some stack overflow answers
public class BTReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BT", "Receive");
String action = intent.getAction();
...
switch (action) {
case BluetoothDevice.ACTION_ACL_CONNECTED:
Intent i = new Intent();
i.setClassName("com.opendashcam", "com.opendashcam.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
And like this
Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
But all that I can see it's just these logs (first two say that receive was gotten and activity was started with connection to written MAC)
D/BT: Receive
D/BT: started app with 00:14:41:1E:26:27
I/Timeline: Timeline: Activity_launch_request time:129879532
My Main Activity's onCreate:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("MA", "Started app");
init();
}

Since Android 10, according to Android Developers docs ,Android does not allow launching an activity from the background:
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
As an alternative, you can show notification that will launch the activity if clicked:
In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity.

Related

What have changes about broadcast receiver on Android 11?

I'm using an Android foreground Service to download files and I'm showing the progress of the download to the user in a Notification.
I also have a progress bar in a fragment in the app.
I'm using a broadcast Receiver to communicate the progress of the download happenning in the service to my fragment.
My problem is that the onReceive of my BroadcastReceiver is not trigger above Android 11 bu works perfectly fine on android 10, What had change about broadcast reveiver since Android 11 ?
On my fragment i'm declaring my broadcast receiver :
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if ("SHOW_PROGRESS".equals(intent.getAction())) {
showProgress(intent.getIntExtra("progress", 0));
}
}
};
And registering it in OnCreateView :
IntentFilter filter = new IntentFilter();
filter.addAction("SHOW_PROGRESS");
getContext().registerReceiver(receiver, filter);
From my service I'm calling it :
Intent broadcastIntent = new Intent().setAction("SHOW_PROGRESS").putExtra("progress", downLoadState.getGlobalPercent());
getApplicationContext().sendBroadcast(broadcastIntent);
I have nothing in my manifest since my service don't interacts with device components

Android using broadcastReceiver, but when I force close the app, I don't get anything in the Activity

Not sure how to get the receiver to work on the activity once the app is forced closed.
What am I missing to get this to work even if the app was forced closed? Any help would be appreciated.
I am getting the BroadcastReceiver service to work, Just not getting anything to pick up on the activity level.
I have my receiver (Service):
public class MyReceiver extends BroadcastReceiver {
public static final String SEND_NOTIFICATION_ACTION = "com.clover.sdk.app.intent.action.APP_NOTIFICATION";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver", "Triggered MyReceiver");
String action = intent.getAction();
Bundle getIntent = intent.getExtras();
if (action.equals(SEND_NOTIFICATION_ACTION)) {
Log.i("MyReceiver Gotten", "Found");
intent = new Intent("broadCastName");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("orderId", getIntent.getString("payload"));
Log.i("Receiver OrderID", getIntent.getString("payload"));
context.sendBroadcast(intent);
}
}
}
My Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(broadcastReceiver, new IntentFilter("broadCastName"));
}
}
Then my broadcastReceiver in my activity:
// Add this inside your class
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver Gotten 2", "Found");
Bundle b = intent.getExtras();
Log.i("MyReceiver Gotten 3", b.getString("orderId"));
new SpecificOrderAsyncTask(MainActivity.this).execute(b.getString("orderId"));
}
};
Not sure how to get the receiver to work on the activity once the app is forced closed. What am I missing to get this to work even if the app was forced closed?
That's contradictory - you can't get a receiver to work in an Activity that registered it at runtime if that Activity that is hosting the receiver is killed. When you force close, every in the app process - including the Activity and the receiver you registered with it - disappears.
The point of calling registerReceiver is to listen for broadcasts only during a specific time frame or lifecycle.
If you want the receiver to work even when the app is closed, don't register it at runtime - register it in the manifest.
Simple,
Registering service in an activity is temporary, registering service in a manifest will run even after closing the application.
But the broadcast you use is a simple message transfer system, that won't work even after you register in manifest and close the application. You have to create a background service that runs always in background in android system and should awake listening to some events passed.

Restart Android app to specific activity

I want to restart my Android app, but I want it to go to a specific activity after the app restart.
I am currently using this code to re launch my app, but it starts back to the first activity. I need it to go to another specific activity.
I made a settings page in my application where users can choose their own app colors. After choosing a color, i need to restart all activities to apply the new theme that the user chose.
code I am using to restart app to first activity:
Intent startActivity = new Intent();
startActivity.setClass(ProfileSettingsActivity.this,ProfileSettingsActivity.class);
startActivity(startActivity);
finish();
easy
Intent intent = new Intent(this, ANOTHER_SPECIFIC_ACTIVITY.class);
this.startActivity(intent);
finish();
In the onCreate() of your first activity, check for whatever conditions you want, and then based on those conditions launch the intent for your specific activity before the setContentView() is called in the first activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(CONDITION_1) {
startActivity(new Intent(this, ActivityA.class));
} else if(CONDITION_2) {
startActivity(new Intent(this, ActivityB.class));
}
setContentView(R.layout.activity_first);
}
Have you tried clearing all activities and launch the new one with Flags?
Intent intent = new Intent(this, NEW_ACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Doing this, you are clearing all activities previously created and launch NEW_ACTIVITY.class with the new configuration.

OneSignal SDK: How to open MainActivity after user taps on notification

How can I open Main Activity if user taps on push notification sent from OpenSignal. I wanted to override the default behaviour which was causing some issue when App was active. I added following line as per the doc
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
Now if app is closed, how can i open MainActivity, and let it execute NotificationOpenedHandler.
Thank you.
If you still always want your launcher / main Activity to open / resume when tapping on a OneSignal notification add the following code to your Activity intead.
private static boolean activityStarted;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( activityStarted
&& getIntent() != null
&& (getIntent().getFlags() & Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
finish();
return;
}
activityStarted = true;
}
See Resume last Activity when opening a Notification instructions for more details.
If you need to do something more custom keep the manifest entry you noted above and add a OneSignal NotificationOpenedHandler to OneSignal.startInit in your Application class.
import com.onesignal.OneSignal;
public class YourAppClass extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
#Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
// The following can be used to open an Activity of your choice.
/*
Intent intent = new Intent(getApplication(), YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
*/
// Follow the instructions in the link below to prevent the launcher Activity from starting.
// https://documentation.onesignal.com/docs/android-notification-customizations#changing-the-open-action-of-a-notification
}
}
See 4. Add Optional NotificationOpenedHandler for more details on this callback.

Call Server function and popup on Broadcast receiver android

I have an application in which I have to call off an alarm/notification each 30 Minutes.
I want the feature like
1- If app is closed, it open the app, Call a dialog box. On click it will call a serverFunction and if MainActivity is running, update its UI.
2- If the app is already opened , Call a dialog box. On click it will call a serverFunction. Since MainActivity is may or may NOT on the top, update its UI Or NOT.
In My MainActivity.class
private void callNotification()
{
AlarmManager service = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, Constants.TIME_CONSTANT);
service.set(AlarmManager.RTC_WAKEUP ,time.getTimeInMillis(), pending);
}
public class AlarmReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
}
}
The problem here is , I can't put a dialog box in onReceive since context is not Activity context. What If the app is opened , Now how am I suppose to implement above features.
In your onReceive place this to call your activity:
Intent i = new Intent(context, AlertActivity.class);
i.setFlags
startActivity(i);
Once you are in your activity you can open up a dialog.
I recommend you use a different activity than your main one to handle displaying the alert, as it makes sense from a design standpoint and it also makes implementation easier. Remember you can make Activities look like dialogs...

Categories

Resources