I am new to android and developing android application with lock functionality.i want to disable device's home button in an activity and i am googled for it from last 4 days and i am not getting correct solution for it.i am tried all the solution that i get in below link,but i am not able to disable home button.please suggest me how to disable home and all device's key programmatically.any help would be appreciated.
here is the link that i visited and tried the solutions that described their.
How to disable android hardware buttons programmatically?
How to lock android buttons/phone from code (screen lock)?
http://sunil-android.blogspot.in/2013/07/mostly-used-android-code.html
http://www.technonutty.com/2013/11/disable-home-power-back-button-android.html
Here is the code that i lastly edited:
MainActivity.java
package com.example.lockdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private LockLayer lockLayer;
private View lockView;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
button1 = (Button)lockView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lockLayer.unlock();
finish();
}
});
}
private void init() {
lockView = View.inflate(this, R.layout.activity_main, null);
lockLayer = LockLayer.getInstance(this);
lockLayer.setLockView(lockView);
lockLayer.lock();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LockLayer.java
public class LockLayer {
private Activity mActivty;
private WindowManager mWindowManager;
private View mLockView;
private LayoutParams mLockViewLayoutParams;
private static LockLayer mLockLayer;
private boolean isLocked;
public static LockLayer getInstance(Activity act){
if(mLockLayer == null){
mLockLayer = new LockLayer(act);
}
return mLockLayer;
}
private LockLayer(Activity act) {
mActivty = act;
init();
}
private void init(){
isLocked = false;
mWindowManager = mActivty.getWindowManager();
mLockViewLayoutParams = new LayoutParams();
mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
}
public void lock() {
if(mLockView!=null){
mWindowManager.addView(mLockView, mLockViewLayoutParams);
}
isLocked = true;
}
public void unlock() {
if(mWindowManager!=null && isLocked){
mWindowManager.removeView(mLockView);
}
isLocked = false;
}
public void setLockView(View v){
mLockView = v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lockdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you.