这是交易,我一直在制作一个简单的屏幕储物柜应用程序,主要执行以下操作:
- 移除钥匙扣
- 禁用主页和返回按钮
- 关闭后屏幕打开时运行
- 在启动时运行 [尚未测试]
我已经完成了我的家庭作业并使用了大量的链接(尤其是在 stackoverflow 上),但仍然有很多问题
1号完美地工作:)
数字 2有效,但没有按预期工作,当我按下它时,它会打开询问用户选择家庭应用程序的对话框......我不想要那个!我想做一个锁屏应用,而不是家庭应用。(代码在最后提供)
数字 3在解锁屏幕之前工作,但之后,应用程序不知道如何自动启动。我已经实现了一个广播接收器,它在 onCreat() 方法的代码中注册,用于锁定屏幕活动。我认为这是问题所在:S
4这根本不起作用!
我正在使用服务进行测试,但仍然无法正常工作:'(
代码
清单我确定我使用了正确的权限,是吗?
<?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的布局很简单,一键解锁:)
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;
}
}
广播接收器这处理 3 件事,屏幕开/关和启动完成(或应该),,,我一直在用 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");
}
}
}
最后,该服务现在没有做任何事情,我试图在第一次解锁后使用它来启动锁定屏幕
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);
}*/
}
我在互联网上发现的更多问题包括在本机键盘保护中打开屏幕后返回状态栏(非常糟糕),有状态栏,但它不起作用,有没有办法做到这一点?
有没有办法让储物柜活动,并告诉系统你来了,用它作为键盘保护?