Proximity screen off
We can learn how to screen off with proximity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import android.content.Context; import android.os.PowerManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private PowerManager mPowerManager; private PowerManager.WakeLock mWakeLock; private static final String TAG = "MainActivity" ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); } public void activateSensor(View v) { Toast.makeText(MainActivity. this , "Proximity On" , Toast.LENGTH_LONG).show(); if (mWakeLock == null ) { mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "incall" ); } if (!mWakeLock.isHeld()) { Log.d(TAG, "New call active : acquiring incall (CPU only) wake lock" ); mWakeLock.acquire(); } else { Log.d(TAG, "New call active while incall (CPU only) wake lock already active" ); } } public void deactivateSensor(View v) { Toast.makeText(MainActivity. this , "Proximity Off" , Toast.LENGTH_LONG).show(); if (mWakeLock != null && mWakeLock.isHeld()) { mWakeLock.release(); Log.d(TAG, "Last call ended: releasing incall (CPU only) wake lock" ); } else { Log.d(TAG, "Last call ended: no incall (CPU only) wake lock were held" ); } } } |
And now in your layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = "com.example.javier.proximitysensor.MainActivity" > < Button android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@+id/activity_main_turn_on_screen" android:onClick = "activateSensor" android:text = "@string/activity_main_activate_sensor" /> < Button android:id = "@+id/activity_main_turn_on_screen" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:onClick = "deactivateSensor" android:text = "@string/activity_main_deactivate_sensor" /> </ RelativeLayout > |