'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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
