how can i stop and start live wallpaper service in android programmatically ?
this code help me to send user for select my live wallpaper from list
if (Build.VERSION.SDK_INT > 15)
{
i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = WallpaperService.class.getPackage().getName();
String cls = WallpaperService.class.getCanonicalName();
i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls));
}
else
{
i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
}
startActivityForResult(i, 0);
is there any way to programmatically change the user's live wallpaper ?
If your are using a test device that have API level below the 15 it will not be possible to change live wallpaper the way you want.! Your code is perfectly alright. Just test on a device that have API level greater then 15/16.!
Related
Again about notification sound on Android O+.
There are some phones where the "notification settings" window doesn't show the sound selection button (and not even the vibration button).
Here are a couple examples:
Samsung A5
Huawei Honor View 10
(not minor brands... I would say)
They were tested with Gmail app (menu -> Settings -> account -> Notification settings) on Android 8.
Here Android O - Notification Channels - Change Vibration Pattern or Sound Type is a solution to avoid the "standard" window, but why should we reinvent the wheel?
Is there any other option that I'm missing?
Thank you,
Max
P.S.
Here is a screenshot from a Honor 9 / Android 8.0.0.
Channel name is "Mail" ("Posta" in Italian). For sound ("Suoneria" in Italian) there is only an On/Off switch.
It's a mess. You need to add workarounds for the different brands/devices. This is the flow we're using to deal with it:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isDeviceWithWorkaround()) {
// Send to notification channel settings (See https://developer.android.com/training/notify-user/channels#UpdateChannel)
}else{
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Sound");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(someExistingRingTone));
if (isDeviceWithWorkaround()) {
intent.setPackage("com.android.providers.media");
}
try {
startActivityForResult(intent, reqCode);
} catch (ActivityNotFoundException e) {
if (isDeviceWithWorkaround()) {
Log.i(TAG, "Failed to find preferred package [" + intent.getPackage() + "]. Trying again without package.");
intent.setPackage(null);
startActivity(intent);
}
}
}
So what's happening is that if it's a device with a known issue as you talk about we send them to the good old ringtone picker.
I believe that the package com.android.providers.media doesn't have an activity to start on stock Android, but on Huawei it then opens the Media Store where we get back a ringtone URI that can be used as notification sound. (We don't want the user to end up in some other ringtone picker that might not work. We have always recommended our users to use https://play.google.com/store/apps/details?id=com.angryredplanet.android.rings_extended but it won't work with Huawei on Android 8).
I'm building an Android media player application that I intend to use to play media (videos, pictures, etc.) on a TV while connected via an HDMI cable.
I want to have the media player app pause when the TV's power status is OFF and want it to play when the TV is turned ON.
How do I detect the TV's power status within my Android application when my Android device is connected to the TV via HDMI?
Both the TV and the Android device have support for HDMI-CEC. The device in question is an ODROID C2. I've seen this functionality on the KODI Android application which has a feature to pause the video when the HDMI-CEC status is OFF, I'm looking to implement this within my app as well.
Any help is appreciated. Thanks in advance!
EDIT: Progress below
I tried reading the status of the HDMI connection from within this file /sys/devices/virtual/switch/hdmi/state. However, this file holds int 1 no matter whether the power status of the connected screen / TV is ON or OFF.
2nd Progress update
I'm still working on this. Will not give up, and once I'm done I will surely post the answer here.
You can listen for changes in HDMI status (0 for unplugged and 1 for plugged) by registering for ACTION_HDMI_AUDIO_PLUG. It reports with status 0 when tv is switched off, switches to any other display medium or HDMI is removed. To read into its technicality, you can check out how hot plug detection works in HDMI. Overall, your app can at all times monitor whether the display can currently play your content or not. I have myself implemented this in a solution (on X96 mini android box & amazon fire-stick) where I needed to ensure that the content was actually being played because it included paid content. Also, I have attached the sample code file.
Note: This solution will only work when android device is HDMI source not sink!
Here's the documentation link too- https://developer.android.com/reference/android/media/AudioManager#ACTION_HDMI_AUDIO_PLUG
private BroadcastReceiver eventReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// pause video
String action = intent.getAction();
switch (action) {
case ACTION_HDMI_AUDIO_PLUG :
// EXTRA_AUDIO_PLUG_STATE: 0 - UNPLUG, 1 - PLUG
Toast.makeText(getApplicationContext(),"HDMI PLUGGED OR UNPLUGGED",Toast.LENGTH_LONG).show();
Log.d("MainActivity", "ACTION_HDMI_AUDIO_PLUG " + intent.getIntExtra(EXTRA_AUDIO_PLUG_STATE, -1));
((TextView)(findViewById(R.id.textView))).setText(((TextView)(findViewById(R.id.textView))).getText().toString().concat("At "+System.nanoTime()+": "+intent.getIntExtra(EXTRA_AUDIO_PLUG_STATE, -1) +"\n"));
break;
}
}
};
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(eventReceiver);
}
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_HDMI_AUDIO_PLUG);
registerReceiver(eventReceiver, filter);
}
In Some TV's, You need to monitor that (sys/class/amhdmitx/amhdmitx0/hpd_state) folder for changes by 500 ms Interval. because it'll change from 1 to 0 and again from 0 to 1 within 1 seconds.
I want to change my android device audio mode from Normal to silent and silent to normal. `
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(!silent) {
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
silent = true;
}
else {
audio.setRingerMode( AudioManager.RINGER_MODE_VIBRATE );
silent = false;
}
`
This is my code that i'm using. Perfectly running until Android 6 all versions.
But on Android 7+ it crashes.
I found a solution on web that opens a settings intent and get some permission from user then this code works fine. It is the code i used then
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
But my project requirement is don't request to open a setting intent to allow it form there. Is should be work like getting user permission to change mode on same activity just like all other permissions i.e. location, read contacts etc.
I tried to find out the find how they implement the permission in the code file but did not find it. Please let me know if anyone know.
This is the only way to change the Audio Mode of Android with api level > 23
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
Now officially its released by Android to get user permission to access Notification Policy of device to change the audio Mode.
How to detect if display is on?
Requirements
Must work on devices with API level >=17 and <20 (Android 4.2.2, 4.3, 4.4)
I am not asking for interactive state like Intent.ACTION_SCREEN_ON / PowerManager#isScreenOn() / #isInteractive()
Must work from inside a Service
With DisplayManager for example
DisplayManager dm = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
return false;
You can use a simple Broadcast receiver for screen sates (ON or OFF).Why don't you wanna use Intent.ACTION_SCREEN_ON ?.
Ref:
enter link description here
Is it possible somehow to set Live Wallpaper programmatically using my Application?
I am working on an Application that her purpose is to choose some of the Installed Live Wallpapers on the device and to set it as a Live Wallpaper. This action need to be completed via my Application.
As I was researching I found some answers that this can be done with rooting the Android Device?
Can some one help me out how to do that exactly?
Android OS prior to Jelly Bean does not allow you to programatically set a live wallpaper.
For now Jelly Bean supports changing the Live Wallpaper programtically without user interaction
Sorry to break it to the nay sayers but it is possible to set a live wallpaper programmatically WITHOUT user interaction. It requires:
Your app to be system-privileged
<uses-permission android:name="android.permission.SET_WALLPAPER_COMPONENT" />
Java reflection (super hacking code)
A class reference to the desired WallpaperService (live Wallpaper)
NOTE: For item #3, I used my own live wallpaper, MyWallpaperService class
This can only be done if your app is system-privileged and has this permission in the manifest:
<uses-permission android:name="android.permission.SET_WALLPAPER_COMPONENT" />
Now, using reflection, you can call the hidden methods of WallpaperManager to manually set the live wallpaper:
WallpaperManager manager = WallpaperManager.getInstance(context);
Method method = WallpaperManager.class.getMethod("getIWallpaperManager", null);
Object objIWallpaperManager = method.invoke(manager, null);
Class[] param = new Class[1];
param[0] = ComponentName.class;
method = objIWallpaperManager.getClass().getMethod("setWallpaperComponent", param);
//get the intent of the desired wallpaper service. Note: I created my own
//custom wallpaper service. You'll need a class reference and package
//of the desired live wallpaper
Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
intent.setClassName(context.getPackageName(), MyWallpaperService.class.getName());
//set the live wallpaper (throws security exception if you're not system-privileged app)
method.invoke(objIWallpaperManager, intent.getComponent());
Refer to the source code:
LiveWallpaperActivity
LiveWallpaperPreview