When selecting a wallpaper in android through the wallpaper selector, the os prompts the user to select from a list of wallpapers and to my knowledge some wallpaper applications, such as Wallpaper Engine, but when setting the wallpaper through code, it seems to be possible to only set a drawable image as a wallpaper. Is there any way to, for example, open this system prompt etc. and allow the user to select the wallpaper or wallpaper application?
Currently it seems it's possible to select images with this method and use them through this method.
I suppose it could be done somehow with the ACTION_LIVE_WALLPAPER_CHOOSER constant specified in android studio, but I wasn't able to find documentation on how to create a system selector prompt with that?
Just to clarify, the application itself is not supposed to be a wallpaper displayer, but rather it only sets the wallpaper drawable or another independent wallpaper drawer application.
Edit: The wallpaper/engine has to be chosen beforehand by the user, after which the app will automatically at some point in time apply it
Edit 2: I tested some methods posted previously on SO, but wasn't able to get them working on API 30, even when trying to copy paste the code directly as a last result.
The code was taken from here and can be summarized as such:
PackageManager m_package_manager = getContext().getPackageManager();
List<ResolveInfo> available_wallpapers_list =
m_package_manager.queryIntentServices(
new Intent(WallpaperService.SERVICE_INTERFACE),
PackageManager.GET_META_DATA);
return available_wallpapers_list;
The returned list is empty even if some additional live wallpaper applications have been installed
If you add this intent filter to your activity in the manifest, it should appear in the chooser:
<activity
android:name=".ChangeWallpaperActivity"
android:exported="true">
<intent-filter>
<!-- This is the important part -->
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can test this with the following bit of code:
Intent intent = new Intent("android.intent.action.SET_WALLPAPER");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "No activity found to handle " + intent.toString());
}
Related
I would like to allow users to highlight text and post it to my web site.
Is there a way to create an Android Intent running as a service which will show up on the social media / share list that appears in the Android system?
You know, when the user selects a picture and then the three dotted share icon appears and gives a list? How can I get my application's intent and icon added to the list just as the Twitter, FB etc. ones that show up?
To receive data from an external application in your application vis Share functionality, you need to create an activity in your application which will accept the incoming data. Add the following details in AndroidManifest.xml for the Activity which will accept the data:
<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
So , now if any application, shares a single image, your application will be visible in the list, and if the user selects your application, ui.MyActivity activity will be launched. You can change or add multiple mimeType as per your requirement.
Also, you can add/change action as MULTIPLE_SEND to receive multiple shared files.
Once this is done, in onCreate() method of your activity, you can fetch the data in the following manner:
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
//Play with the data, as per your need
...
}
You can get more details regarding this, in the Official Documentation
Background
According to a new feature on Android M (link here), apps outside your app can offer to give a direct sharing intent to one of its activities, allowing, for example, a chatting app to share the content to an exact contact, so you choose both the chatting-app and the contact at the same time (one step instead of 2) . This can be shown on this image:
Or, at least that's what I've understood from it.
The question
I have 2 questions regarding this new feature:
In the description, they only show what to put in the manifest, and they mention using "ChooserTargetService". What should be done in order to provide the texts and images?
I'd like to know how to do the opposite : how can I query all of those "direct-share" items (images, texts, and intents) and be able to show them on a customized dialog?
I want to do it because I have a customized dialog myself, that allows to choose what to share and how, and not just through which app.
Question 1
In the description, they only show what to put in the manifest, and
they mention using "ChooserTargetService". What should be done in
order to provide the texts and images?
Start by extending ChooserTargetService. You'll need to return a List of ChooserTarget and how you create those targets is entirely up to you.
public class YourChooserTargetService extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
final List<ChooserTarget> targets = new ArrayList<>();
for (int i = 0; i < length; i++) {
// The title of the target
final String title = ...
// The icon to represent the target
final Icon icon = ...
// Ranking score for this target between 0.0f and 1.0f
final float score = ...
// PendingIntent to fill in and send if the user chooses this target
final PendingIntent action = ...
targets.add(new ChooserTarget(title, icon, score, action));
}
return targets;
}
}
AndroidManifest
Now you'll need to declare your ChooserTargetService in your AndroidManifest and do two things:
Bind the Service using the android.permission.BIND_CHOOSER_TARGET_SERVICE permission
Include an IntentFilter with the android.service.chooser.ChooserTargetService action
For example:
<service
android:name=".YourChooserTargetService"
android:label="#string/yourLabel"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
In the Activity that's going to handle the Intent, you'll need to add the meta-data tag android.service.chooser.chooser_target_service. For example:
<activity android:name=".YourShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".YourChooserTargetService" />
</activity>
From here, it's mostly a matter of calling Intent.createChooser and then handling the data if the user chooses your application.
final Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TITLE, "Your title");
target.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(Intent.createChooser(target, "ChooserTargetService Example"));
Results
Things to note
The ranking score for each ChooserTarget is used to sort the targets, but is only used if the UI decides to use it. As per ChooserTarget.getScore
The UI displaying the target may take this score into account when
sorting and merging targets from multiple sources
Also, as far as I know, this feature isn't actually implemented yet in the Android MNC preview. The ChooserActivity contains a TODO for it:
TODO: Maintain sort by ranking scores
When creating a new android.graphics.drawable.Icon, you'll need to use one of the static initializers.
Icon.createWithBitmap();
Icon.createWithContentUri()
Icon.createWithData()
Icon.createWithFilePath()
Icon.createWithResource()
Question 2
I'd like to know how to do the opposite : how can I query all of those
"direct-share" items (images, texts, and intents) and be able to show
them on a customized dialog?
The data supplied to ChooserTargetService.onGetChooserTargets is dynamic. So, there's no direct way to access those items, as far as I know.
I have different understanding of this future.
Until now when user wanted to share something they been asked to choose the application they want to share with, and then this application handled the share.
Now instead of user choosing the application they will choose the content from the application that will handle the share. Each such option is encapsulated in android.service.chooser.ChooserTargetService.
So as you see on the image, it shows some products of ChooserTargetService, the user see some contacts ui without lunching or sharing just yet.
I believe your dialog could be triggered at the same way.
I have found several examples of Android apps that have simple widgets that are droppable into home screen folders and the bottom, persistent row on the home screen. For instance, the Gmail label widget, or the Dropbox folder widget. In my attempts to make a similar widget - single cell with icon and text view underneath - the widget can only be by itself on the home screen.
How can I make the most simple widget that can be dropped into a home screen folder or home row just like a normal app icon?
Is it possible what I'm looking for is actually not a widget, but an app icon that would somehow be included in the widget list (but without adding a second app icon in the app list)?
EDIT:
Huge thanks to Kuffs for getting me in the right direction.
The key to this is in what Kuffs points to in the manifest:
...
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
...
As Kuffs mentions, that alone gets your app an entry in the widget list. Good work team.
Widgets can only be placed on the home screen. They cannot be added to the dock on the home screen. Only icons go here.
You need to add a shortcut.
Add the relevant permission to your manifest.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Create an activity that will create the shortcut and add it to the manifest like the example below.
e.g
<activity
android:name=".ShortcutActivity"
android:label="#string/app_name" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If you run this app now, you will be offered the opportunity to add your shortcut like the Gmail/Dropbox apps do (The widget screen will list your new shortcut creating activity). Of course, your activity should actually add the shortcut to be useful.
The activity will be started by the OS with startActivityForResult() and therefore it will be expecting an intent inside the returned intent (to be used by the OS in an onActivityResult call). This info found here: https://stackoverflow.com/a/11449443/1399483
So, create an activity that creates a shortcut such as this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i= new Intent();
Intent shortcutActivity = new Intent(this, ActivityToLaunch.class);
i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutActivity);
i.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Title");
i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.shortcut_icon));
setResult(RESULT_OK, i);
finish();
}
To use a custom icon rather than a drawable resource, replace the extra EXTRA_SHORTCUT_ICON_RESOURCE with the following:
Bitmap bm; // Set to the image you want to use
i.putExtra(Intent.EXTRA_SHORTCUT_ICON, bm);
Hmm, good question! It's certainly true that widgets, even though 1x1, can't be placed inside a folder or the dock.
Maybe it really isn't a widget that's created after selecting the label/folder, but instead a shortcut is made. The 'widget' is a shortcut and the way to put the shortcut on your homescreen is trough the (select label/folder) 'widget'.
With the right permission and code you can create a shortcut from your sourcecode.
In my application I have an option to start navigation to selected POI. Basically what I want is to launch a turn-by-turn navigator from my application. The thing is I don't know which (if any) navigator is installed.
So, the question is how to start an intent by showing a list of suitable activities for navigation to the user first, letting him choose which one he would like to use? Also would be nice to find a way to pass extra parameters to selected activity (this sounds like an issue to me, since different navigation apps use different names for their extras, I guess).
In case it's not clear: I'm looking for a way to DISPLAY A LIST OF SUITABLE APPLICATIONS FOR NAVIGATION WITH THE OPTION TO MAKE ONE DEFAULT.
EDIT: Find here the implementation http://datamoil.blogspot.com/2011/04/android-universal-intent-to-start.html
The bad news is, there isn't a standard Intent URI for navigation.
Yes, google.navigation URIs exist, and an app may choose to support it.
The best solution I can think of is to:
Explicitly check for known apps
Implicitly check for apps hooking google.navigation: and perhaps geo: (but then you also get map apps)
You can enumerate the possible implicit targets using PackageManage.queryIntentActivities
Try:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=New+York+NY"));
startActivity(intent);
First I used in my onTap method inside the button's listener:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=New+York+NY"));
mContext.startActivity(i);
Then in manifest simply use:
<activity android:name=".LaunchGPS" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This will open any Navigator App your phone has such as VZ Navigagtor, Google or whatever the phone is loaded with. Worked for me the first time perfectly. Hope this solves your problem.
I found more advantages using this code (just for to show available navigator apps)
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"));
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Continues with:", CHOOSE_NAVIGATOR_ID);
} else {
// Handle failure...
}
In developer.android.com I read central feature of android is you can make use of elements of other applications in your own application.Iam interested in this line.
1)How can we use the .apk downloaded suppose from android market in to our application ?
2)How can we use our own created applicaton into newly created application?
please give me guidance on this and if possible sample snippet?
Regards,
Rajendar
I guess you mean that:
A central feature of Android is that
one application can make use of
elements of other applications
(provided those applications permit
it). For example, if your application
needs to display a scrolling list of
images and another application has
developed a suitable scroller and made
it available to others, you can call
upon that scroller to do the work,
rather than develop your own. Your
application doesn't incorporate the
code of the other application or link
to it. Rather, it simply starts up
that piece of the other application
when the need arises.
Two last sentences are crucial. And the link provides more information about it. But briefly: application author can write his code in a manner that it can be reusable for others. He/she can do that by putting "intent filters" into his/her application's AndroidManifest.xml . E.g. Google's Camera application (the one that provides camera functionality and image gallery as well - yeah, the application can "expose" many "entry points" = icons in the home screen) has activity definition (one of many) as follows:
<activity android:name="CropImage" android:process=":CropImage" android:configChanges="orientation|keyboardHidden" android:label="#string/crop_label">
<intent-filter android:label="#string/crop_label">
<action android:name="com.android.camera.action.CROP"/>
<data android:mimeType="image/*"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.ALTERNATIVE"/>
<category android:name="android.intent.category.SELECTED_ALTERNATIVE"/>
</intent-filter>
</activity>
That means that one can use it's cropping the image functionality by sending intent:
/* prepare intent - provide options that allow
Android to find functionality provider for you;
will match intent filter of Camera - for matching rules see:
http://developer.android.com/guide/topics/intents/intents-filters.html#ires */
Intent i = new Intent("com.android.camera.action.CROP");
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("image/*");
/* put "input paramters" for the intent - called intent dependent */
i.putExtra("data", /*... Bitmap object ...*/);
i.putExtra( /*... other options, e.g. desired dimensions ...*/ );
/* "call desired functionality" */
startActivityForResult(i, /* code of return */ CROPPING_RESULT_CODE);
CROPPING_RESULT_CODE that one can define in one's Activity is used to distinguish which external activity returned (helpfull if one calls several external functions) and is provided in calling activity's onActivityResult() method which is called when "remote" application finishes - below an example:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CROPPING_RESULT_CODE:
/* ... crop returned ... */
if (data != null) {
/* get cropped bitmap */
Bitmap bmp = data.getParcelableExtra("data");
/* ... and do what you want ... */
}
case ANOTHER_RESULT_CODE:
/* ... another external content ... */
}
}
Other options are using: other Services or ContentProviders.
If you have any more questions don't hesitate.
Android uses Intents to allow applications request work be done by software residing in other applications. See my answer to this question for more details on Intents and an example of how your application can ask the Browser application to display a webpage.