I'm having a MainActivity, which start a foreground service as an intent with the methode ContextCompat.startForegroundService this service has an instance of paho MQTT connection.
When I closed the application, I want to send a last message in MQTT to warn that the application going to be closed, but this didn't work all the time, namely when I'm not connecting to my computer with the android studio debugger.
So have you any idea to help me ?
More informations :
On the onDestroy() methode of MainActivity :
getApplicationContext().stopService(intent);
On the onDestoy() of my service :
client.published(client.GetTopic(),client.TAG_CLOSE);
try {
client.client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
Log.e("destroy service", "ERROR !!!!");
}
EDIT : I discovered that the problem, appear when I turn on the battery saving.
Neither the onDestroy method nor onStop is guaranteed to be called.
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it.
source: https://developer.android.com/reference/android/app/Activity.html#onDestroy()
I believe this could change from device to device. Your observation that it happened while the battery saving option was ON could be very true in this case.
As for the solution maybe take a look at bound services and see if they are better fit for your scenario: https://developer.android.com/guide/components/bound-services
Stay strong and happy coding!
After the help of Kostek (https://stackoverflow.com/a/71485487/16188409), I found the solution, by adding the "REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" to the manifest as the post below said :
https://stackoverflow.com/a/67770107/16188409
Related
I'd like to force stop my Android application when I click closeButton. This is my code.
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.layoutxml);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
This finishes my application. If I go to Settings -> Applications -> Manage applications -> <my application name>, I can see the 'Force Stop' button is enabled. Does this mean my application was not stopped completely?
How can I finish my Android application completely and disable the 'Force Stop' button inthe 'Settings'? From my limited experience, when an 'Exception' (ex. NullPointerException) occurs in the application, it stops abnormally, looks like it finished completely, and the 'Force Stop' button looks disabled.
Another way is
android.os.Process.killProcess(android.os.Process.myPid());
I don't think it's all that bad to do this, provided you put those calls in onDestroy(). (If you kill your process in the middle of event handling, all kinds of bad things—like the touch focus going into the ether—can happen.)
Nevertheless, you need a compelling reason to deviate from best practice, which is to just call finish() and let the OS take care of killing off your process when/if it needs to.
Note: This does not kill the entire app, but if what you want to do is to finish all the app activities, this is the best option.
Android ≥ 16
finishAffinity();
Android < 16
ActivityCompat.finishAffinity(Activity activity)
Hope this helps
A bad way to kill the application would be System.exit(0)
Edit:
I believe I owe some explanation. Android handles the application lifecycle on its own, and you are not supposed to 'ForceClose' it, and I don't know any good way to do it. Generally its ok if your application is still alive in the background, this way if user launches it again it will pop up quickly.
I know it is a late reply , hope this helps some one.
You can try finishAndRemoveTasks(); instead of finish(); in your snippet.
This would kill your application's all activities and all process and even remove for recent apps from task manager.
Note: If you have use any kind of handler or thread in your code make sure you remove its functionalities and then use the above suggested code , if not NullPointer Exception or ResourceNotFound Exception would occur.
Short and simple
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
Why not to make a Shell-Call to ActivityManager?
try {
Runtime.getRuntime().exec("am force-stop com.me.myapp");
} catch (IOException e) {
e.printStackTrace();
}
The link below has the solution.
Its worked for me.
finishAffinity()
How to force stop my android application programmatically?
I'm looking for a way to close an android app after some time while the app has not been in focus. For example if the user open up an other app instead, the app should exit after 5 mins. I have tried using runnable and creating a thread. But those method don't seems to work while the app is in the background (maybe they are pause I'm not sure). So how do I close the app when it is not in focus?
For those who are wonder the reason I want to do this is that the app contains some sensitives data about the user so I want to be sure it is all cleared when they aren't using it.
Something like this might work:
A field inside activity class:
private Thread t = null;
Inside onResume():
if(t!=null) {
if(t.isAlive()) {
t.interrupt();
t.join();
}
t=null;
}
Inside onPause():
t = new Thread() {
public void run() {
try {
sleep(5*60*1000);
// Wipe your valuable data here
System.exit(0);
} catch (InterruptedException e) {
return;
}
}.start();
}
I recommend calling finish() in the onPause() or onStop() callbacks. A TimerTask will not survive onPause() and a Service does not appear, on face value, to give you options. Maybe you can start a service, sleep the thread the service runs on, then kill the processes your app has after the sleep timer expires.
Alternatively, you can just implement some security libraries to help secure the data from other apps.
Here is the Google Services link.
Get the process ID of your application, and kill that process onDestroy() method
#Override
public void onDestroy()
{
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}
Refer- how to close/stop running application on background android
Edit- Use this with AlarmManager
The fundamental problem with what you're trying to do is that your Activity may not exist in memory at all when it's "running" in the background. The Android framework may have destroyed the activity instance and even the process it was running in. All that exists may be the persistent state you saved in onSaveInstanceState(...) and a screenshot for the recent apps list. There may be nothing for you to get a reference to and kill.
Frank Brenyah's suggestion to call finish() in onPause() will prevent your activity from running in the background at all, but this is the closest you can get to what you want. You probably only want to do this when isChangingConfigurations() is false. But even when all your app's activities are finished, Android may keep the process and Application instance around to avoid recreating them later. So you may also want to use Bhush_techidiot's suggestion of killing the process. Do this in onPause() because the activity may be destroyed without a call to onDestroy().
I need to understand how a message traverses the Android system from the service level down to the driver calls.
I have decided to start with WiFi, although my current hardware platform - a Beaglebone Black board with TI's Rowboat (Android 4.2.2) - does not have WiFi capability.
My plan is to insert logging function calls to the source code and send an Intent to WifiService through the am program in order to trigger the messages towards the driver.
I have found, in init.rc, that ServiceManager is started through the following code
service servicemanager /system/bin/servicemanager
class core
user system
group system
critical
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
onrestart restart drm
Furthermore, from frameworks/base/services/java/com/android/server/SystemServer.java, I have found this code snippet
try {
Slog.i(TAG, "Wi-Fi Service");
wifi = new WifiService(context);
ServiceManager.addService(Context.WIFI_SERVICE, wifi);
} catch (Throwable e) {
reportWtf("starting Wi-Fi Service", e);
}
which, I believe, starts WifiService.
However, I cannot determine how SystemServer is started.
Besides that, I managed to insert a log function at frameworks/base/wifi/java/android/net/wifi/WifiManager.java, as follows
public WifiManager(Context context, IWifiManager service) {
mContext = context;
mService = service;
Log.i(TAG, "WiFiManager Created. Calling init()"); // inserted log
init();
}
and I can see it working through the use of logcat -s WifiManager, which results in
I/WifiManager( 317): WiFiManager Created. Calling init()
I/WifiManager( 396): WiFiManager Created. Calling init()
I/WifiManager( 968): WiFiManager Created. Calling init()
Nevertheless, I cannot determine why it is called 3 times or where the calls came from.
As for C code, I hope to see its log messages by using dmesg.
I have cscope and ctags configured to browse the code but I cannot manage to find what comes below WifiService or WifiManager.
It looks like I still have a long road ahead, so any help would be greatly appreciated.
Slide 8 from this presentation might be able to answer a couple of your questions
The sources for the WifiService and WifiController might be of help as well, you can find them in frameworks/base/services/java/com/android/server/wifi. Also check the README.txt in there.
I'm now developing an android application with the NFC concept . In that application I need to pick my application if I swipe the NFC card and if I select my application my application must start calling webservice.
when it comes to my problem, If suppose my app crashed ,when I swipe the card next time ,the option to choose the application doesn't open .Instead,my application directly launched and couldn't call the webservice data.
On the whole.I'm getting last page when it crashed .But I need to open as fresh
I came to know that I need to make changes in OnResume() and OnNewIntent().
I used ,
In OnResume()
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mNdefExchangeFilters, null);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] msgs = getNdefMessages(getIntent());
byte[] payload = msgs[0].getRecords()[0].getPayload();
//Toast.makeText(this, new String(payload), Toast.LENGTH_LONG).show();
Student=new String(payload);
if(Student.equals(rakesh)
{
new Webservice.execute(""); // Calling SOAP Webservice
}
But,I can't find any changes with my problem .and one more thing that the problem will be resolve after I just open and close an another NFC project
Please help.
Yeah ! I got the solution .I forget a simple thing that I left calling onStop() method and my problem was,when my application stops(when Crashed).It runs in background as the previous activity.
I just tried the following,
public void onStop()
{
super.onStop();
this.finish();
}
This may be helpful to others.
Thanks..
I have been developing an app, and I need to close another app in my code. Does anyone know any api to call to close an app?
BTW: my app will be pre-installed.
thanks
Since Android 2.2 (i.e. going forward), you can only close the background processes of other apps, you are no longer able to close their main activities.
If your app is targeting Android <2.2, look atandroid.permission.RESTART_PACKAGE.
If you want it to work properly on 2.2 and above (which you should :-)), look at android.permission.KILL_BACKGROUND_PROCESSES, but again, this only closes background services and such and might "mess up" the other app rather than doing any good.
With the right permissions, you can then do the following:
private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");
Try This
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService("activity");
Method forceStopPackage;
forceStopPackage =am.getClass().getDeclaredMethod("forceStopPackage",String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, pkg);
In manifest file add this
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
If both applications are yours, you can use AIDL for inter-process communication to send a message telling the other application to close. See http://developer.android.com/guide/developing/tools/aidl.html.
I have been able to close another app on Android 12 successfully. Here is how:
Basically, I am closing another app from a service although you should be able to do it from an app too.
My service is a privileged system app that gets installed in system/priv-app/ (It has LOCAL_PRIVILEGED_MODULE := true in its Android.mk)
I added <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" /> in AndroidManifest.xml
I added in privapp-permissions.xml
<privapp-permissions package="<my service package name>">
<permission name="android.permission.FORCE_STOP_PACKAGES"/>
</privapp-permissions>
I called in my service this method with the package name of the application I want to close:
private void closePackageApp(String namePackage) {
ActivityManager activityManager = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);
try {
Method forceStopPackage = activityManager.getClass().
getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(activityManager, namePackage);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
I tested this and in the logs, I can see the app is being closed. However the app is not removed from the recent screen (logs suggested the app was disposed without first being removed with the input manager!).
However, I am sure the app was really being closed when it was in the background by comparing its lifecycle on opening again. Normally, it is onPause->onResume but now it is onPause->onCreate.
You don't ever really want to close another application, due to Android activity lifecycle.
There's no benefit, and always detriment to closing another app if it's not yours, and very little benefit to closing your own.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If you know for certain that you'll never, ever need a root activity and its children (an "app"), you can stop it to free memory (it doesn't free that much), but if you do the user may restart it while it's still in cache, stopped, which can cause problems if the stopped state is restored. So this is a bad practice.