Verify app close ANDROID - android

I am conducting a background program that runs from x in x seconds to get new information. So far so good. The problem is that when I finish the application thread remains always active.
Is there any way to check if the application has ended?
thank you

You could make a Toast or AlertDialog box along with your code that finishes the activity to let you know that the application has ended.

If your task should stop when the activity stops being shown, you should not start the service but bind it to the activity. The task then will be automatically stopped as soon as the activity has finished. If multiple activities should use the service, bind it to each of them.
Here's how to bind the service:
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

Related

Android - Not able to start multiple activities from background

I am facing a problem.
This is the code I have in my BroadcastReceiver extender class:
#Override
public void onReceive(Context context, Intent intent) {
// other
Intent myIntent = new Intent(context, ShowMessageActivity.class);
myIntent.putExtra(Utils.SHOW_MESSAGE_OPTION, messageToDisplay);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(lockScreenMessage);
}
which starts a new activity when a broadcast is raised.
When the application is in foreground and receives a broadcast, it starts a new Activity as many times as the code executes, but it is not the case when the app is in background. In that case it starts an Activity only once, and not each time a broadcast is received. Why? Is it possible to fix that?
that very depends on what do you expect in that receiver, is it any system-side call (declared in manifest) or your own? who an when calls it, thats very important
starting Android 10 there are some restrictions for starting Activity from onReceive, check out official DOC. basically you shouldn't start any Activity from there, and when it works for you I bet it works only for few secs after PendingIntent creation. check out exceptions list under link above, possible reasons would be:
The app has an activity in the back stack of an existing task on the Recents screen.
The app has an activity that was started very recently.
The app called finish() on an activity very recently.
you may check out your code on some emulator with Android 9, all your broadcast calls should work "always"

Android app seems to be duplicating when I swipe it away and start it again

I am currently having a really weird issue, and I don't know what could be causing it. Every time I close the app by swiping it away, and then start it again, the app seems to be duplicating its output to logcat, or in other words, it appears to be running multiple times.
This doesn't happen when I am connected to Android Studio, but without a cable connection, it does it without fail.
In my main activity I start my service like this:
Intent intent = new Intent(this, BluetoothService.class);
startService(intent);
bindService(intent, bluetoothServiceConnection, Context.BIND_AUTO_CREATE);
Stopping the service:
unbindService(bluetoothServiceConnection);
Intent intent = new Intent(MainActivity.this, BluetoothService.class);
stopService(intent);
EDIT I have made some changes in the way I stop and start my service, which seems to have solved my problem on Android 5.1.1, but not on Android 4.4.4, sadly.
I was thinking that maybe my logging process could be the problem, but destroying that process in my activity's onDestroy()-method didn't solve the issue either.
Starting the logging process:
logger = Runtime.getRuntime().exec(new String[]{"logcat", "-v", "time", "-f", logcatFile.getAbsolutePath(), " *:E"});
Stopping it:
logger.destroy();
If you start a service, depends how you define your service in the AndroidManifest.xml, if you have android:process=":yourProcess", if the process name start with ":" it will create a new process, that probably your log output multiple times.
Your code doesn't contains any logging information, nor did you provide a stacktrace with it. Your splash screen doesn't seem to be the issue, but you should replace the new thread with a delayed post to a handler to the looper thread (main thread) you're running in:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
}, 1500);
Instead of getApplicationContext() you could also use SplashActivity.this directly.
In your MainActivity you then need to make sure to unbind the service. Maybe also stop it, depending on what you need it for. Refer to Bound Services for more documentation and examples.
For making the Service terminate the documentation states
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.
That's why it works when the service isn't started at all. You try to stop the service in onDestroy() which is only called if the service is stopped. Your code looks like the service doesn't need to be started at all. But to do it correctly, just stop the service in the acivity's onDestroy() or utilize
onUnbind() or any other useful lifecycle event of the activity or service.

Can an activity be triggered immediately when a notifcation arrives?

I need to implement this feature but I am not sure if this is possible on android.
We have gcm listener service running. But let's say the app is open, and a notification arrives from the server. I need to have an activity triggered automatically without touching the notification status bar on the top of the phone screen.
In other words, without any user interaction once the notification arrives, if the app is running, an activity must be triggered immediately.
I took a look at this thread, but this is not really what I need.
Intent - if activity is running, bring it to front, else start a new one (from notification)
any clues or more info?
thx!
You can start an activity without another prior activity by using the FLAG_ACTIVITY_NEW_TASK flag.
Context c = getApplicationContext(); // or getContext(), or any other context you can find from the current app
Intent i = new Intent(c, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

How to Stop bind local intent service

I have made the Intent service and designed it in such a way that it has capability of restart itself if killed by the OS , but if it is stopped by user it must have stop.
Every thing was going good. But Problem occured when I tried to implement message handler and Binding techniques.
What I have done
My sole purpose was getting the status from the service (which in my case is intent service) and update my views. For this I have implemented Message handler and start to bound the service .
I am binding my service in such a way
//This is my interface to get specific call back in activity
mReceiver = new DownloadResultReceiver(new Handler());
mReceiver.setReceiver(ListSuitClass.this);
serviceIntent = new Intent(DownloadService.ALARM_SERVICE);
serviceIntent.setClass(ListClass.this, DownloadService.class);
serviceIntent.putExtra("receiver", mReceiver);
serviceIntent.putExtra("requestId", 101);
startService(serviceIntent);
}
if (!mIsBound) {
bindService(serviceIntent, mConnection, BIND_DEBUG_UNBIND);
mIsBound = true;
}
Problem
I want to stop the service by stop button , But it stop for a while and starts over again .
I do not know what I am doing wrong , And what is starting again the service.
Please help me in stopping the service.
Note: I am running my service in a separate process.
As per the code, mIsBound might be the local variable of Activity. When you stop your activity and restart it, it will have the default value of boolean i.e. false and so bindService is called again and again. What you need to do is either make it static variable or store the service bind status in SharedPreferences and read value from it.

After rotation change second instance of service

My app is a video player which streams videos from a nas. Therefore the video is also playing in the background, I have running a startforeground service where the media player is hold.
So every time the activity starts I have to bind to the service, to be able to show the video. This is also required when rotation changed. Then when I want to bind to it, sometimes I don't bind to the already running service but It creates a new instance. So there are two instances of the service.
Yes, normally services should only be able to be instantiated one time, but in my case there are sometimes definitly 2 instances... :/
How can I prevent this? Had anybody already the same problem?
EDIT:
service gets started and bound with following code:
Intent serviceIntent = new Intent(getApplicationContext(), MediaPlayerService.class);
getApplicationContext().startService(serviceIntent);
Intent serviceIntent = new Intent(getApplicationContext(), MediaPlayerService.class);
getApplicationContext().bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
Sorry I wasn't clear because I was interested in which method in the activity life cycle you were doing this in. There are certain methods that are called on-orientation change. This other answer should point you in the right direction.I hope that helps.
Which activity method is called when orientation changes occur?

Categories

Resources