Here’s the deal, I’ve been working on making a simple screen locker app that mainly does the following:
- removes the keygaurd
- disables home and back buttons
- runs whenever the screen goes ON after going OFF
- runs at boot [not tested yet]
I’ve DONE MY HOMEWORK and used tons of links (especially on stackoverflow) and still so many problems
number 1 works perfectly 🙂
number 2 works but not as intended, when I press it, it open the dialog that asks the user for choosing a home application… I don’t want that! I want to make a lock screen app, not a home app. (code is provided at the end)
number 3 works before unlocking the screen, but after that, the application doesn’t know how to start itself automatically. I’ve implemented a Broadcast Receiver that is registered in code in the onCreat() method for the lock screen activity. I think this is the problem :S
number 4 this doesn’t work at all!
I was testing with a service, but still not working :'(
Code
Manifest I’m sure I’m using the the right permissions, am I?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="himura.test.mylockertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<receiver
android:name=".EventsReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name=".UpdateService"/>
<activity
android:name=".LockPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
activity the layout is very simple, just one button to unlock 🙂
public class LockPage extends Activity {
private Button ublockButton;
@Override
public void onAttachedToWindow() {
//this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
///** FIRST THINGS FIRST, START THE SERVICE **/
//startService(new Intent(this, myService.class));
/** REGISTERING RECEIVER **/
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
BroadcastReceiver mReceiver = new EventsReciever();
registerReceiver(mReceiver, filter);
/** SETTING CONTENT VIEW**/
setContentView(R.layout.lockscreen);
/** REMOVING KEYGUARD RECEIVER **/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
/** NORMAL CODE **/
ublockButton = (Button)findViewById(R.id.bUnlock);
ublockButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
@Override
public void onBackPressed() {
// Don't allow back to dismiss.
return;
}
//only used in lockdown mode
@Override
protected void onPause() {
super.onPause();
Log.i("event","onPause");
// Don't hang around.
finish();
}
@Override
protected void onStop() {
super.onStop();
Log.i("event","onStop");
// Don't hang around.
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
}
Broadcast Receiver this handles 3 things, screen on/off and boot complete (or it should),,, I’ve been testing with Logs
public class EventsReciever extends BroadcastReceiver {
//works before unlocking
//after unlocking, keygaurd still off, but receiver has stopped
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent recievedIntent) {
Log.i("Check","[BroadCastReciever] onRecieve()");
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.i("Check","[BroadCastReciever] Screen went OFF");
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.i("Check","[BroadCastReciever] Screen went ON");
Intent intent = new Intent(context, LockPage.class);
context.startActivity(intent);
}
else if(recievedIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent intent = new Intent(context, LockPage.class);
context.startActivity(intent);
// Intent intent = new Intent(context, LockPage.class);
// context.startActivity(intent);
// Intent serviceLauncher = new Intent(context, UpdateService.class);
// context.startService(serviceLauncher);
// Log.v("TEST", "Service loaded at start");
}
}
}
Finally, the service doesn’t do anything now, I was trying to use it to start the lock screen after it gets unlocked for the first time
public class myService extends Service{
@Override
public void onCreate() {
/** INITIALIZE RECEIVER **/
//RegisterReciever();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The standard pattern for implementing a Service is to create and run a new thread from onStartCommand
// to perform the processing in the background and stop the Service when it’s complete
//RegisterReciever();
return Service.START_STICKY;
}
/*private void RegisterReciever(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
BroadcastReceiver mReceiver = new EventsReciever();
registerReceiver(mReceiver, filter);
}*/
}
more issues I’ve found on the internet includes the return of the status bar after turning the screen on (very bad)
in the native keyguard, there is the status bar, but it doesn’t function, is there a way to do that?
isn’t there a way to just make the locker activity, and tell the system that here u go, use this as the keyguard?
If you create your own firmware, presumably there is a way to replace the keyguard with an alternative implementation, considering that most device manufacturers do it. You cannot replace the keyguard via the SDK.