[Q] Stop another Apps Services - Java for Android App Development

Need help, I'd like to stop another Apps Service. For example, TuneIn has a service I want to stop. I'm trying this approach, but it does not work:
Intent stopIntent = context.getPackageManager().getLaunchIntentForPackage("tunein.player");
//stopIntent.setPackage("tunein.player");
Boolean res = context.stopService(stopIntent);
I know its doable because this App can do it: Service Killer: package: jp.imxs.skr.servicekiller
Does anyone know the correct way to accomplish this?
..cp2

Related

Activities, Services, and BroadcastReceivers.. whoa. Advice?

I'm working on an application that will use a Service Controller (Activity) to set personal settings, and then run a Service in the background constantly to take action on your preferences, based on system changes.
I'm trying to wrap my head around getting said changes. It sounds like I need a BroadcastReceiver class--but it also appears that I could simply add the <intent-filter> element to my Manifest for my Service (or even my Activity) and get the same results. Is that true? Can I simply create a BroadcastReceiver object within the Service to obtain broadcasted Intents?
I understand that BroadcastReceivers are essentially dormant until an Intent is broadcast, but it seems like unnecessary overhead as compared to having my already-running Service analyzing broadcasts for applicable data.
Additionally, if BroadcastReceivers are the ideal (or only) way of handling broadcast Intents, is transporting data to my Service as another (Explicit) Intent the only way to do so? It appears that I'd have to use the "ExtraData" fields, and that seems.. worthless? I want to transport my own structure with flags for the values I care about, rather than creating a string (or other specific layout) to interpret values from the "ExtraData" field.
Thoughts on BroadcastReceivers? Insight from the more advanced developers would be appreciated.
Thanks for your time.

Detect Package Launch

I am working on an application and I need to be able to have a service that can detect when applications are opened, and take action based on that.
I have looked everywhere, and have not found a way to do so. I have read documentation on broadcast receivers, intent handlers, I found nothing.
I did however, find an app that can do so. App Protector. It allows password protecting certain applications.
Can anybody please point me in the right direction?
Thanks!
this is actually a very good question. a BroadcastReciever only gets Intents directed at it, and wouldnt see Intents for other classes and Packages.
ill be very interested to know the answer
killersnowman said:
this is actually a very good question. a BroadcastReciever only gets Intents directed at it, and wouldnt see Intents for other classes and Packages.
ill be very interested to know the answer
Click to expand...
Click to collapse
Well I was looking for a BroadcastReceiver that was sent out by the system to indicate that an app was opened, but I did not come across anything.
this isnt possible.. to be able to monitor all intents would make android extremely insecure
http://groups.google.com/group/android-developers/browse_thread/thread/54ddc9d36a24d77b
but there are ways to know when an application is launched. you just have to be creative.
this will give you a list of all the applications running.
Code:
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
however to know when an app is launched you would need a timed loop and then check between versions of the List to see if there is a new app. this would suck the juice and be inneficient
AppProtector seem to access the eventlog. maybe you could have a ContentObserver attached to the event log
http://developer.android.com/reference/android/util/EventLog.html
http://developer.android.com/reference/android/database/ContentObserver.html
killersnowman said:
this isnt possible.. to be able to monitor all intents would make android extremely insecure
http://groups.google.com/group/android-developers/browse_thread/thread/54ddc9d36a24d77b
but there are ways to know when an application is launched. you just have to be creative.
this will give you a list of all the applications running.
Code:
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
however to know when an app is launched you would need a timed loop and then check between versions of the List to see if there is a new app. this would suck the juice and be inneficient
AppProtector seem to access the eventlog. maybe you could have a ContentObserver attached to the event log
http://developer.android.com/reference/android/util/EventLog.html
http://developer.android.com/reference/android/database/ContentObserver.html
Click to expand...
Click to collapse
Interesting.
I also found this:
Code:
String str = ((ActivityManager.RunningTaskInfo)this.am.getRunningTasks(1).get(0)).topActivity.getPackageName();
It's one of the things App Protector uses
It starts a new intent, pointing to their application, and passing the parameters of the package you tried to run
Take a look at Tasker it can defiantly do this.
rujelus22 said:
Take a look at Tasker it can defiantly do this.
Click to expand...
Click to collapse
He doesn't want it for scripting, but for use in his own program.
From something awesome

uitilizing power of root

Hello all i have a questio?
Does anyone know how do i get a service to accuire root and prevent any taskmanager apps from killing it i dev andriod app but dont know about root and all
I dont want to start another service to monitor each other and start the other in case one is killed
sak-venom1997 said:
Hello all i have a questio?
Does anyone know how do i get a service to accuire root and prevent any taskmanager apps from killing it i dev andriod app but dont know about root and all
I dont want to start another service to monitor each other and start the other in case one is killed
Click to expand...
Click to collapse
If I want to use root i simply do something like that
Code:
Runtime.getRuntime().exec(new String []{"su","-c",yourcommand});
As for unkillable service maybe you can set it foreground, but I am not sure it works though:
Code:
Notification notification=new Notification(0,null,System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(2, notification);
DoR2 said:
If I want to use root i simply do something like that
Code:
Runtime.getRuntime().exec(new String []{"su","-c",yourcommand});
As for unkillable service maybe you can set it foreground, but I am not sure it works though:
Code:
Notification notification=new Notification(0,null,System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(2, notification);
Click to expand...
Click to collapse
Ya I've tried this android system won't kill but taskmanager can easily kill it and for my application it is essential that this service is not stopped as it monitors information from sensors
The root part
This much I also know what command would I give
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
Ya I've tried this android system won't kill but taskmanager can easily kill it and for my application it is essential that this service is not stopped as it monitors information from sensors
The root part
This much I also know what command would I give
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
I don't think thats possible. I don't think you can prevent other apps from killing your service.
You could however try to restart service from its own onDestroy() method.
This is just theoretical but this way when something stops service it should re-start itself.
Again I have no idea if this is possible, it's just an idea.
Root task managers that actually kill process(using kill or killall )are completely different matter.
Sent from my Evo 3D GSM using Tapatalk 2
Antivirus services don't get killed unless used a root taskmanager
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
when a service is started with START_STICKY then it will automatically be re-started by the Android system. That's why auto-task killers are a horrible idea, because they kill your service over and over again, and drain your battery easily.
http://developer.android.com/reference/android/app/Service.html#START_STICKY
Unfortunately there's no solution to stop task killers.
You mean there's no way out
Its important for scenarios like mine I'm dumping heavy data to a sql or mysql data base on the cloud that is done only in onDestory() method if done else where would make low memory devices crawl and if a taskmanager kills it I lose all data stored in ram even if it's restarted by the system
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
You mean there's no way out
Its important for scenarios like mine I'm dumping heavy data to a sql or mysql data base on the cloud that is done only in onDestory() method if done else where would make low memory devices crawl and if a taskmanager kills it I lose all data stored in ram even if it's restarted by the system
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
Maybe you can make something like that:
YourService.java
Code:
@Override
public void onDestroy() {
startService(new Intent(context,Dummy.class));
}
Dummy.java
Code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService(new Intent(context,YourService.class));
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
I don't know if it is the best way, but I think it will work
DoR2 said:
Maybe you can make something like that:
YourService.java
Code:
@Override
public void onDestroy() {
startService(new Intent(context,Dummy.class));
}
Dummy.java
Code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService(new Intent(context,YourService.class));
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
I don't know if it is the best way, but I think it will work
Click to expand...
Click to collapse
Hmm when taskmanager kills the service onDestory us not called that's my problem.
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
You should never do important things in OnDestroy as there's no guarantee that the system is calling it, even if no task killers are used. I guess you should rethink your design and push the data earlier, cache it locally, etc.
ramdroid77 said:
You should never do important things in OnDestroy as there's no guarantee that the system is calling it, even if no task killers are used. I guess you should rethink your design and push the data earlier, cache it locally, etc.
Click to expand...
Click to collapse
System never kills a service it only kills activities Google says
activities like dumping data to a sql database should be done in onDestory() method
Well that wasn't the topic I want to prevent taskmanager apps (non root) from killing my service
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
System never kills a service it only kills activities Google says
activities like dumping data to a sql database should be done in onDestory() method
Well that wasn't the topic I want to prevent taskmanager apps (non root) from killing my service
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
I don't know what your service is exactly doing but permanent foreground services should only be used if you really need them on all the time, e.g. during music playback. According to the docs non-foreground services (like you should normally use) might still get killed by the system after some time...
Otherwise, what happens if the user force stops your application? Or if the phone is shutting down? You shouldn't do any life-depending things in onDestroy, you only want to release allocated resources. Uploading data to the cloud in onDestroy seems plain wrong to me!
Yes, auto-kill task managers are bad, but I think that's not the only problem here
ramdroid77 said:
I don't know what your service is exactly doing but permanent foreground services should only be used if you really need them on all the time, e.g. during music playback. According to the docs non-foreground services (like you should normally use) might still get killed by the system after some time...
Otherwise, what happens if the user force stops your application? Or if the phone is shutting down? You shouldn't do any life-depending things in onDestroy, you only want to release allocated resources. Uploading data to the cloud in onDestroy seems plain wrong to me!
Yes, auto-kill task managers are bad, but I think that's not the only problem here
Click to expand...
Click to collapse
Then where should I write to the database if on in onDestory
And if you force stop the app using Android settings the on destroy is called
Only if it's done with a taskmanager it won't
What my service does it to track the network sites visited by the user in order to bill them accordingly after all we won't sponsor data usage other than official use in our country isp's are not permitted to release info about user other than to the government so if a user kills it with a taskmanager he's off the radar and if database is stored locally the user might tamper with the data
If i write data to sql server periodically the system would crawl as I'd be first compressing it to save data and it's a huge file I will hold data from other in house apps too
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
If you are looking for a way of writing a root app, try this library: http://code.google.com/p/roottools/
At the beginning I tried it without that lib and it worked. However, you will need to keep your shell open if you do not want it to ask for permission again everytime you need to execute a command. And this needs some code. That is why I use roottools now. It is much easier and we can concentrate on our app instead of getting these background things to work.
If your app needs to execute commands not very often, you can ask for the permission every time, but it slows down your application for many commands and there will be annoying Toast messages every time. The advantage would be not having to put some other people into the credits.
That is my experience. I hope that it is helpful.
Well my best idea is to let the user know that your app needs to run the service, so he shouldn't kill it with any task manager. I use root with this code:
Code:
try {
Process proc = Runtime.getRuntime().exec("su");
DataOutputStream DOS = new DataOutputStream(proc.getOutputStream());
//write what you want to send to it here. Example:
DOS.writeBytes("ls /data/data/");
DOS.flush();
DOS.close();
proc.waitFor();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Rotary Heart said:
Well my best idea is to let the user know that your app needs to run the service, so he shouldn't kill it with any task manager. I use root with this code:
Code:
try {
Process proc = Runtime.getRuntime().exec("su");
DataOutputStream DOS = new DataOutputStream(proc.getOutputStream());
//write what you want to send to it here. Example:
DOS.writeBytes("ls /data/data/");
DOS.flush();
DOS.close();
proc.waitFor();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Click to expand...
Click to collapse
At first I did it that way, too. One bad thing with this solution is that everytime you execute a command it will ask for permission again and there will be a new Toast message. If your app has to execute commands multiple times, keeping the shell open is more performant. That is the advantage of roottools.

[Q] Hack for rooted S5 to keep GPS on regardless of toggle?

Is there such a mod? Need to locate some of my business phones regardless of what the user sets the location setting to.
hockeyfreak said:
Is there such a mod? Need to locate some of my business phones regardless of what the user sets the location setting to.
Click to expand...
Click to collapse
I can't believe that you tried searching for an answer before posting as this has been asked and answered more than once in the forums before.
GPS is a heavy draw on battery and this is an niche request. Given that it is for business use, perhaps you should consider paying someone to make you a custom app.
Alternately, go look in the Play store to see if there is an existing app that does what you want.
Or you could use the xposed framework or tasker to hook onto the method that sets the GPS in the settings.
Or set a BroadcastReceiver service to listen for any change to the GPS status and renable it if changes. e.g.
Code:
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) {
// if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings","com.andr oid.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
/*
* Toast.makeText(getApplicationContext(), "GPS On Success",
* Toast.LENGTH_SHORT).show();
*/
Code untested by me
Credit to ken.okech.94
.

How to know what app is foreground and do something when it goes background

So, I'm developing an application for Android which will only be used in my own couple tablets which are going to public use.
I need an application which keeps track which (possibly 3rd party)application is foreground, and when that application goes to background the service starts activity which will go foreground and show something to the user(for example like some ad, or some review window which asks for start rating).
In Android 5.0 access to logcat is restricted so it needs to be done trough accesibility services I guess? Or is there way to use custom ROM with access to logcat? Or could rooting open it?
Thank you.
As for logcat, root allows access to it
You can get running apps using this piece of code
Code:
ActivityManager am = (ActivityManager) mContext .getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity .getPackageName();
and use an if conditition and a thread to keep checking, at the moment its not meeting the condition the desired code will be triggered

Categories

Resources