Is there anyway to detect hthe camera button in Sleep mode? I tried the examples explained in this forum, but nothing works in Sleep mode.. I am llooking for this for a long time, but no proper answers..
How I can receive hardware key events in sleep mode?
Please help me...
public class YourBoardcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())) {
Intent main = new Intent();//
}
}
}
And in your Manifest :
<receiver android:name="YourBoardcastReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
This is not possible. When the device is put in sleep mode, almost all apps are paused or stopped. Only enough of the processor is kept on to be able to receive calls and SMS texts, and manage alarms and notifications. Apart from that, pretty much everything is discontinued.
If you want to keep detecting it when the screen is off, you will need to acquire a wakelock to prevent the device from going into sleep. However, if you do this all the time it will have an impact on the users' battery life.
Related
I'm making an Android TV and Amazon Fire TV app that uses WAKELOCK to prevent the TV device from going to sleep. What I need to do though is release the WAKELOCK when the screen gets turned off, e.g. when someone presses the power button on the TV, as in this case the Amazon Fire TV Stick etc stay active although the TV is powered off.
I then need to re-add the WAKELOCK when the TV is powered on. What is the accepted best practice for handling this?
EDIT: as per comment I'm updating this response with the most effective method.
In a nutshell you can achieve this in two ways:
Check if the HDMI gets disconnected (mainly works on phones, keep reading for TV)
Check if the audio channel becomes noisy. As per Android documentation (https://developer.android.com/guide/topics/media/mediaplayer.html#noisyintent) you can do something like the following (change with ):
"You can ensure your app stops playing music in these situations by handling the ACTION_AUDIO_BECOMING_NOISY intent, for which you can register a receiver by adding the following to your manifest:
<receiver android:name=".MusicIntentReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
This registers the MusicIntentReceiver class as a broadcast receiver for that intent. You should then implement this class:
public class MusicIntentReceiver extends android.content.BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().equals(
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
// signal your service to stop playback
// (via an Intent, for instance)
}
}
}
Trying to monitor the offline state of the app by using a broadcast receiver on android.net.conn.CONNECTIVITY_CHANGE but I’m experiencing a huge delay in the connection change when switching on and off the airplane mode, 20 - 30 seconds, is there any faster way? (without polling the connection)
Manifest:
<receiver android:name=".service.NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
Receiver:
public class NetworkStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "\t\t\tCONNECTIVITY CHANGED");
}
}
EDIT: there is a D/NetworkChangeNotifierAutoDetect: Network connectivity changed, type is: 5 log in the logcat that is almost instant, is there a way to capture something that triggers that log?
EDIT: found that android.intent.action.SERVICE_STATE is actually way quicker for reconnection. Dropping connection change still 20 to 30s delay :(
I have a notification being fired through AlarmManager and the notification also
plays a sound.
Obviously, it may happen that the alarm is fired when the app is in the background, and I would like to let the user cancel the sound when pressing the lock button - i.e. listening for ACTION_SCREEN_OFF.
Therefore I wonder if it's possible to start a service and listen for ACTION_SCREEN_OFF?
I have seen Listening for ACTION_SCREEN_OFF but that solution of having a BroadCastReceiver only seems to work when the app is in the foreground. Right?
For instance if you are trying to do ACTION_SCREEN_OFF then you would define your broadcast receiver in your Activity that started the alarm for instance.
public class SomeListener extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//Turn off sounds
}
}
Then in the manifest provide something of the sort like this within the activity that uses the listener.
<intent-filter>
<action android:name="android.hardware.usb.action.ACTION_SCREEN_OFF" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.ACTION_SCREEN_OFF"
android:resource="#xml/my_filter" />
Where the extra my_filter class could provide additional meta-data. In this case it was a check against a Serial or UUID of the device so not to launch on all USB connects. But you should be able to do something similar.
When the action occurs, this should fire the listener within your application, as I understand it anyways. This feature works for launch of application on USB connect for me in the past. Even without having had the application open in the first place.
I am building a device for the blind.
I want to re-purpose a low end android device for the task. In terms of hardware I need
Single physical home button (to initiate interactions everything else is done via speech in & out
Reasonable mic and speakers
reasonable amount of cpu and memory
rootable (we need to root to be able to do things like power down the device)
3G data
I have it working on a variety of devices but the best fit is the samsung pocket but it has one problem which I am unable to resolve.
When the user powers on the device it does not give any non visual feedback. Most devices vibrate when you power them on. The blind person needs to able determine that they have held the button long enough. The pocket doesnt provide any non visual feedback and what is worse if you hold it down for too long it powers down again.
Is it possible to change this behaviour. I am hoping it is in the boot rom. I am rooting the pocket using update.zip (home/vol_up/power ...) so I can do almost anything there but I have never created a custom boot rom.
I noticed that it comes pre-installed with odin if that makes it easier
I am a little out of my comfort zone so any advice is greatly appreciated.
Andrew
Here is what you need. Create a BroadCastReceiver like this snippet:
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent();
it.setAction("your.package.name.MyBroadcastReceiver");
context.startService(it); //Start a service
}
}
And add this to your manifest:
<receiver android:name="your.package.name.MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
Then create a service like this
public class YourService extends Service {
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 300, 0 };
v.vibrate(pattern, 0); // Use this to vibrate
// rdlVibrator.cancel(); Use this to stop the vibration
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
And last add this to your manifest file:
<service
android:name="your.package.name.YourService"
android:process=":my_process" >
<intent-filter>
<action android:name="your.package.name.YourService" >
</action>
</intent-filter>
</service>
If you have any question just ask. Hope it helps.
You can create BroadCast receiver that when phone turn on it is called and it's OnReceive method is called and you can write code of enabling device vibration.
I have a little confusion with broadcast receivers. I have a broadcast receiver which is triggered upon TIME_SET and TIMEZONE_CHANGED actions (the code is given below). What I was wondering is, can OnDateTimeChanged (see the code below) be triggered simultaneously (and its execution overlaps) when both TIME_SET and TIMEZONE_CHANGED actions are triggered or is one always going to be triggered after the other? Based on some simple experiments I did, I got the impression that the two executions of OnDateTimeChanged are triggered consecutively with no time overlap but I cannot be 100% sure of this. If anyone has an idea I'll be very happy.
<!-- Excerpt from manifest -->
<receiver android:name=".OnDateTimeChanged">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
</intent-filter>
</receiver>
// Broadcast receiver class
public class OnDateTimeChanged extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Do some work here
}
}
BTW, both TIME_SET and TIMEZONE_CHANGED can be triggered when under Settings - Date&Time you switch to the Automatic mode and this changes both the time and the timezone.
-Ali
Logically, they would all execute simultaneously. Physically, only one can occupy a core at a time and might finish before another starts. Under identical conditions,the behavior might appear to be consistent. The documentation itself describes it as, "All receivers of the broadcast are run in an undefined order, often at the same time."
If you want to give other receivers a chance to run, you can call Thread.yield().