How to delete / remove a Library programmatically? - java

I've integrated some library from the third party into my android app.
They use background service with the location, but it caused java.lang.IllegalStateException error since Android 8 has updated the background location limits.
I've tried to add the following lines to avoid the error but it fails:
if(Build.VERSION.SDK_INT <= 25) { // below Android 8.0
PushAd.startPush(activity);
}else{
PushAd.disablePush(activity);
}
dependencies {
implementation 'com.adlocus:library:3.5.7#aar'
}
Can I delete or remove the library, or stop the service in the main activity programmatically?
[Solved]
I discovered a possible solution to solve the problem, we can use PackageManager to enable/disable a Service/Receiver/Activity from third party library.
ComponentName component = new ComponentName(context, MyReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
Thanks Varun's answer.

Can I delete or remove the library... programmatically?
No.
Can I... stop the service in the main activity programmatically?
Possibly. Ask the developers of the library.
I've tried to add the following lines to avoid the error but it fails
Nobody knows what "it fails" means. Contact the developers of the library and ask them how to use it with Android 8.0+ devices.

I discovered a possible solution to solve this problem, we can use PackageManager to enable/disable a Service/Receiver/Activity from third party library.
ComponentName component = new ComponentName(context, MyReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
Thanks Varun's answer.

Related

List the all apps in Android

I'm trying to list all apps in android device with queryIntentActivities method but the list doesn't return all the apps , It returns only three of them. Here is my code:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo ri : allApps) {
Log.d("Labels", String.valueOf(ri.loadLabel(packageManager)));
}
is there anyone now why it returns only 3 applications ?
You are most likely trying to do this on Android 11. Make sure you add the <uses-permission android:name"android.permission.QUERY_ALL_PACKAGES"> permission to the AndroidManifest.xml file.
While I haven't tested this aspect of R DP2 yet, it appears that your
app now can't find out what other apps are installed, on a general
basis. The cited example is queryIntentActivities(), but to make this
really work you would need to seriously lobotomize PackageManager. You
can whitelist certain packages and certain structures
to try to get by this for certain use cases. And, this is where the
mysterious QUERY_ALL_PACKAGES permission seen in DP1 comes into play —
this permission removes these new restrictions. Given the "look for
Google Play to provide guidelines for apps that need this permission"
caveat, it is safest to assume that if you try using it, eventually
you will be banned from the Play Store by a bot.

Any way to close recent apps in code?

I want to build upon my battery saving android app and add the functionality to close all apps excluding user-specified ones.
Im not asking how to kill a process, I want to close the recent apps in the same way that you would by hitting your recent apps button and swiping them all away.
Does anyone know of a way to do this?
It is not recommanded but it is posible by using killBackgroundProcesses of Activitymanager.
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
//get a list of all installed apps.
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
Hope it will helps you.

Android - Call an activity of an integrated application

Mixare has an application (Open source) that lets you view POIs with your camera. It gives you the possibility to call the app from your application thanks to this :
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("http://ws.geonames.org/findNearbyWikipediaJSON"), "application/mixare-json");
startActivity(i);
The problem is that user must have the app installed in addition to my app, so what I did is that I imported the whole app within mine, with all its resources and stuff.
But I don't know how to call the main activity MainActivity.java, which resides in the package org.mixare.
How can I make an intent to call this activity ? And how do I declare it in the manifest ?
If you have added the code and resources of the app to your own app, then you should declare and call it's activities as they were your own.
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
This being said, it's not a trivial task. You need to merge AndroidManifest and could get into trouble if you don't know what you're doing. For instance, user can have the Mixare app in addition to yours and intent could have same actions etc.
There is an alternative to this. You could check if Mixare app is installed and if not ask user to do so. This could be more "android way of doing things", depending on your use case.
Look at,
http://code.google.com/p/mixare/wiki/DisplayYourOwnData for how to start mixare via Intent.
Alternatively, you can use mixare as your library project and then call its MainActivity class directly from your application as Using an Android library project Activity within another project.
Quoting the same here -
Declaring library components in the manifest file
In the manifest file of the application project, you must add
declarations of all components that the application will use that are
imported from a library project. For example, you must declare any
, , , , and so on, as well as
, , and similar elements.
Declarations should reference the library components by their
fully-qualified package names, where appropriate.
Then you can definitely call,
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
No, it is very hard to do a 2+2=4 kind of addition of manifest files etc.
I see there are two ways to handle this:
Use the external app: Check if the user has external app you want him to have. Else, direct him to the right link. You can get the package name of the publiched app and use it in this function:
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
Combining code: This has no direct/correct answer. You need to study the code and integrate with your existing one.
//appPackageName,appClassName can be found in Logcat
ComponentName component = new ComponentName("appPackageName","appClassName");
Intent intent = new Intent();
intent.setComponent(component);
startActivity(intent);

How to set theme in AlertDialog in Android

So I've been looking for about an hour and a half, but I can't figure it out. I do NOT want to set a custom theme. I want to set a theme that is built into android for an AlertDialog.
According to d.android.com I can do this:
public AlertDialog.Builder (Context context)
or this
public AlertDialog.Builder (Context context, int theme)
So I do this and it works perfectly:
AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this).create();
But, when I need to add a theme I get an error from eclipse:
AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_TRADITIONAL).create();
I am still very new to programming if someone could help me out on how to set a theme it'd be appreciated.
I also have a bonus question:
I can't get just AlertDialog() to work, to make it work I need to type AlertDialog.Builder(), but on the developer website they both seem to have the same methods and constructors. What's the difference/why doesn't AlertDialog() just work?
Wrap the theme into the context, this is available since API level 1.
Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(
this,android.R.style.Theme_DeviceDefault_Light_Dialog));
Themes for the AlertDialog.Builder are only available for Android 3.0 and newer (API level 11). It seems you are have set an earlier Android version in your project settings.
The Android references show the API level of all constructors and methods. You can even set a filter to show you only the methods available for your API level.
Read more about API levels here.

Possible to skip track from an Android application?

I'm planning on doing a application for Android 2.1 that changes song every minute (through what I hope exists in Android, "next") for the application using the audio device atm.
So if I have Spotify running in background already, playing music, can I through my program change to the next track?
Let me know if I was unclear about anything.
Thanks in advance!
I know this is a bit old question, but it took me some time searching something other then what is mentioned here.
There is a workaround - broadcasting media button action. There is one catch - receiver can recognize if the broadcast was from system or from another app, so they can ignore the non-system broadcasts.
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
}
There's no universal audio transport API for music applications, so you'd need to see if the music applications you're targeting publicly expose service bindings or intents. If not, you won't be able to do this.
Just posted a relevant answer here
Using the AudioManager's dispatchMediaKeyEvent() method with a defined KeyEvent worked for me using the latest SDK.
The system music homescreen widget sends this intent for the built-in music player:
final ComponentName serviceName = new ComponentName(context,
MediaPlaybackService.class);
intent = new Intent(MediaPlaybackService.NEXT_ACTION);
intent.setComponent(serviceName);
pendingIntent = PendingIntent.getService(context,
0 /* no requestCode */, intent, 0 /* no flags */);
views.setOnClickPendingIntent(R.id.control_next, pendingIntent);
But it looks like this might take some hackery to implement outside packages in the music app itself because the MediaPlaybackService only accepts explicit Intents and isn't accessible from the outside. This thread seems to indicate it's possible with a bit of hackery, though.
But even then, as Roman said, not every music player will respect that Intent. You'll have to check with Spotify/Pandora/Last.fm themselves and see if they have any available intents to bind like that.
Looks that it's possible to use AudioManager to inject media keys.
Here is a snippet from another question
this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
long eventtime = SystemClock.uptimeMillis();
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);
The same way you can inject PlayPause button and some others.
I've tested it within a background service controlling Youtube and it worked for Android 6

Categories