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
Related
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.
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.
I have two apps each with an accessibility service. The accessibility service of one app sends a broadcast and the accessibility service of the other app receives it.
Here is the service sending the broadcast:
Intent intent = new Intent();
intent.setAction("com.corps.mypackage");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is needed if broadcast not being sent from activity
sendBroadcast(intent);
And here is the service that receives the broadcast:
IntentFilter intentFilter = new IntentFilter("com.corps.mypackage");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Do some things
}
};
registerReceiver(receiver, intentFilter);
The broadcast is not being received. What could be the reason?
As per the Android documentation:
Context-registered receivers receive broadcasts as long as their registering context is valid. For example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.
Your receiving app's context is likely not valid. Consider using a manifest-declared receiver instead.
I found that in the manifest can not be registered (intent-filter battery low, battery changed).
I need to get the notification when the battery is fully charged. when I have a running app so it is working, but when I close the application so it does not work.
I need the receiver to run in the background when the application is closed.
Main.class
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mContext.registerReceiver(this.batteryInfoReceiver,filter);
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
...
}
My Application required bluetooth connectivity. And in the first phase I am trying to open up the standard Activity "Bluetooth Device Picker" to help user scan for new device or chose a device from the list.
The problem is that I am unable to get any working example for the bluetooth device picker. The task is simple. To start an Activity with Intent "android.bluetooth.devicepicker.action.LAUNCH"
And the device picker is opening without any problem.
But the device picker requires four extras and I am unable to figure out the exact parameters for two of the extras listed below.
.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter"
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity")
What I thought the parameters should be that
*"android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"*
should have the name of my package, so I passed that only. That is "com.extreme.controlcenter"
The second should be the name of the component that must receive the broadcast that is done after a device is selected. Here I tried putting the name of the class that has the onReceive() function.
But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker!
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Device Selected on Device Picker
if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) {
//context.unregisterReceiver(this);
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
String MAC = device.getAddress();
//Log.d("my", MAC);
Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class);
intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC);
startActivity(intent2);
}
};
I have created an Intent filter and registered a receive in the onCreate() of the main Activity
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
registerReceiver(mReceiver, filter);
One thing is that if I don't provide those two extras, the Broadcast event is received successfully. But that code only runs on my TAB, but same is crashing in cell phone. So I think providing those two extras are mandatory.
Thanks in Advance !
"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). I also have it declared in the manifest inside the tags