20

I had written one receiver to detect device motion is changed or not like this in Manifest.xml

 <receiver android:name="com.hanuman.sensor.receiver.SensorReceiver" >
            <intent-filter
                android:enabled="true"
                android:exported="false" >
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

and inside receiver onReceive() method code is:

String action = intent.getAction();
        if (action.equals(Intent.ACTION_USER_PRESENT)) {
            System.out.println("User is present");
            Intent s = new Intent(context, MainActivity.class);
            s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(s);
        } else {
            System.out.println("User is not present");
        }

finally my question is, is not detecting the sensor when it is motioned, it is detecting when my device in unlocked then it is calling my MainActivity, but i want to detect when my device motion is changed then i want to detect in receiver. How can i do that?.

Hanuman
  • 908
  • 14
  • 35
  • Is your CPU awake or asleep? Although you are receiving your broadcast, but you are not able to determine motion, to do so you need to wake up your CPU if its gone to sleep mode. To start with [check this](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html) – Skynet Apr 13 '15 at 11:49
  • if it is in the sleep mode then my device motion is not detected? – Hanuman Apr 13 '15 at 11:52
  • If your device is in sleep mode - sadly no, wake up your device use a partial wakelock. But be sure to release resources once you are done with your operations. It is battery critical. – Skynet Apr 13 '15 at 11:53
  • ok fine.If my device is in wake up mode then how it will detect motion? – Hanuman Apr 13 '15 at 11:55
  • You need to code your motion sensor. Call that code from your onReceive. – Skynet Apr 13 '15 at 11:56
  • If i will write code inside activity then every time it will open activity. – Hanuman Apr 13 '15 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75103/discussion-between-allu-and-skynet). – Hanuman Apr 13 '15 at 12:01

2 Answers2

10

Get object of SensorManager like below-

SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);

Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

Register listener to listen changes in motion sensor-

sensorMan.registerListener(context, sensor,
        SensorManager.SENSOR_DELAY_UI);

Then override onSensorChanged() method to detect changes-

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
    //do some stuff
    }
    //do some other code for other Sensor type
    }
Mohit Rajput
  • 459
  • 3
  • 15
1

You cannot and you should not do that, because when the screen goes off the sensor's gone off automatically, this is done by the system in most of the devices, some devices support sensors listening after the screen goes off like nexus 4.But you can create the receivers that will listen to screen off and screen on.

public class Screen extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOn();
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff();
        }

    }


    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    intentFilter.addAction(Intent.ACTION_SCREEN_ON);
    mReceiver = new Screen();
    registerReceiver(mReceiver, intentFilter);
Lakhwinder Singh
  • 5,247
  • 3
  • 20
  • 36