first off - let me just say that I am NOT asking how to implement a share button in my app or anything like that. I know all about using Intents and Intent Filters etc etc.
what I AM asking about is this: is there any way to get access to the "Share" menu itself? in other words, I'd love to build an app that filters out some of the services I never use but that I don't want to delete from my phone completely.
I tried looking it up in the Android API, but only found info on getting your app to show up in the menu or putting a 'Share' button in your app etc.
Being that I'm still somewhat of a novice programmer, I'm also wondering if there's some way for me to sniff out the API objects that are being created/used when the 'Share' menu is built/displayed? Seems like I could do it in a Debugger session, but I'm not sure how.
Thank you in advance.
b
Well, there are two ways to go around Share menu. First one is to use
startActivity(Intent.createChooser(Intent, CharSequence)
But in this case, I am not sure how to obtain an access to the created share menu, coz it is a separate activity.
However, if you wish to have a control over the list of share items being displayed for your app, there is another way to approach your share menu item implementation.
Take a look at this code snippet:
//Prepare an intent to filter the activities you need
//Add a List<YourItemType> where you going to store the share-items
List<YourItemType> myShareList = new List<YourItemType>;
PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
int numActivities = activities.size();
for (int i = 0; i != numActivities; ++i) {
final ResolveInfo info = activities.get(i);
String label = info.loadLabel(packageManager).toString();
//now you can check label or some other info and decide whether to add the item
//into your own list of share items
//Every item in your list should have a runnable which will execute
// proper share-action (Activity)
myShareList.add(new YourItemType(label, info.loadIcon(packageManager), new Runnable()
{
public void run() {
startResolvedActivity(intent, info);
}
}));
}
This code snippet shows how to get a list of the activities which are able to process share request. What you need to do next is to show your own UI. It is up to you what you are going to choose.
Related
When I open the Android main menu on my Android smartphone, I get a set of apps like Youtube, Calculator, Email clients etc. No system stuff or any libraries are visible there.
To retrieve these apps programamtically, I do:
PackageManager.getInstalledApplications(flag: Int)
where I get a list of ApplicationInfo, which also contains alot more than mentioned installed standard apps. What flag do I have to set to get only the same apps, which I see when I swipe up on my Smartphone?
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val appList = context.getPackageManager().queryIntentActivities( mainIntent, 0)
You can try this to get all user apps (and the ones your launcher shows)
You might need to add additional permissions in your manifest above Android 10 though
Edit: If you want ApplicationInfo instead of ResolveInfo in your list you can retrieve it like this:
appList[your_index].activityInfo.applicationInfo
So if you have ever used the Bitmoji keyboard, you know that it shares your selected Bitmoji in whatever application you are using, be it messenger or SMS without the normal sharing intent pop up, where you choose what application to share it with. This is really my first Android project, as I have been developing for iOS. iOS makes it easy in this case as you just copy the image to the clipboard (pasteboard) and then the user pastes it wherever they want.
Now I'm developing a Android IME, and need to know of a way to know what application the user is currently typing in?
So can someone point me in the direction? I've learned a lot about Android development over the last week, and my head is kind of swimming from reading so much of the documentation, especially with ContentProvider and having images share to the Android SMS application correctly.
Try the below code to get the package name:
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{
String mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
I wanted to post the answer to this question in case someone is in need of the solution. First you need to use the EditorInfo class of the InputMethod to get the packageName of the current InputConnetion using :
EditorInfo editorInfo = getCurrentInputEditorInfo();
String inputConnectionPackageName = editorInfo.packageName;
Then you need to query your intent activities to see what applications the user has that will handle the intent you are trying to send. Using :
PackageManager pManager = getPackageManager();
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
mApps = pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
Then all you will need to do is create a For loop and compare:
for (int i = 0; i < mApps.size()-1; i++) {
ResolveInfo info = mApps.get(i);
if (info.activityInfo.packageName.equalsIgnoreCase(editorInfo.packageName)) {
intent.setPackage(info.activityInfo.packageName);
//send out the intent
startActivity(intent);
}
}
Can someone please provide an example for a real case where I might need to use OnProvideAssistDataListener. I can't seem to wrap my head around it. I look at the source code, and then I look online. Someone online says
Application.OnProvideAssistDataListener allows to place into the
bundle anything you would like to appear in the
Intent.EXTRA_ASSIST_CONTEXT part of the assist Intent
I have also been reading through the Intent Docs.
There is an Now On Tap functionality implemented by Google. By long pressing the Home Button, you will get some information displayed on the screen. The information you get depends on what you're viewing on your screen at that time. (for eg: Music app displays information about music on the screen).
To provide additional information to the assistant, your app provides global application context by registering an app listener using registerOnProvideAssistDataListener() and supplies activity-specific information with activity callbacks by overriding onProvideAssistData() and onProvideAssistContent().
Now when the user activates the assistant, onProvideAssistData() is called to build a full ACTION_ASSIST Intent with all of the context of the current application represented as an instance of the AssistStructure. You can override this method to place anything you like into the bundle to appear in the EXTRA_ASSIST_CONTEXT part of the assist intent.
In the example below, a music app provides structured data to describe the music album that the user is currently viewing:
#Override
public void onProvideAssistContent(AssistContent assistContent) {
super.onProvideAssistContent(assistContent);
String structuredJson = new JSONObject()
.put("#type", "MusicRecording")
.put("#id", "https://example.com/music/recording")
.put("name", "Album Title")
.toString();
assistContent.setStructuredData(structuredJson);
}
For more info refer https://developer.android.com/training/articles/assistant.html
I want to implement a listview showing android applications with their internet usage. Fir this, first i have to list all the apps, i have done this using PackageManager, like this:
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));
But this code lists all system apps as well like : Android Sytem, Calculator,Calender, Status Bar, Live Wallpapers etc. which doesnt look appropriate. I tried to filter system apps using:
/*To filter out System apps*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if(!b) {
packageList1.add(pi);
}
}
But then the code displays only installed apps, like whatsapp, tango, foursquare etc. It does not show apps like gmail, facebook, browser,maps.
Can anybody suggest how should i write the code that only displays list of application that actually use the internet. Thanks in advance!
I want to implement a listview showing android applications with their
internet usage.
An anybody suggest how should i write the code that only displays list
of application that actually use the internet
One solution (maybe only one that works best and came to my head) is to use TrafficStats class that calculating data (TCP, UDP) transferred through network. Exactly in your case, you need to get data for each UID (each application has own UID).
All what you need to know if application trasfered more that zero bytes through network and when you know that, you can tell that "this application uses network".
Here is pseudo-code you could use:
List<Application> collection = new ArrayList<Application>();
Application app = null; // some custom object is good approach
PackageManager pm = getActivity().getPackageManager();
for (ApplicationInfo info: pm.getInstalledApplications(
PackageManager.GET_META_DATA)) {
// received data by application
long downloaded = TrafficStats.getUidRxBytes(info.uid);
// transmitted data by application
long uploaded = TrafficStats.getUidTxBytes(info.uid);
// filter system applications only
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// check if application has network usage
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
// non-system application
else {
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
}
It's important to say that TrafficStats is available from API 8 and also Before JELLY_BEAN_MR2, this may return unsupported on devices where statistics aren't available. I used this approach and never had a problems.
Note: Also I want to mention that maybe there are another possible approach(es) for example reading from some system files but this is (at least for me) hardcoded approach and i don't recommend to use it (also in various devices files can be on different places, have different content and different filename).
I hope it will help you solve your problem.
Application use internet will need Internet Permission
You can filter out those app by checked PackageInfo.permission
Let's say, there are four apps in the system: app1, app2, app3, app4.
Be default, when the system is up, all apps will be shown in the home screen. Now if we provide a customized log in screen, user A log in, then for this user, he can only see (and use ) app1 and app2.
Then A log out, user B log in, he can only see app3 and app4.
Does API provide such capability to load the app list dynamically?
Hope someone can help, thanks.
I think the answer depends on how you build your logging system. But, in theory,the basisc around applications list in Android system would be something like that :
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = mContext.getPackageManager().queryIntentActivities(intent, 0);