ASH*_*ASH 30
使用此方法禁用Android中的Home键
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Run Code Online (Sandbox Code Playgroud)
ami*_*ser 22
我找到了解决HOME键的方法.对于您的应用程序,将清单设置为
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Run Code Online (Sandbox Code Playgroud)
现在您的应用程序是备用Launcher应用程序.
使用adb,并使用包管理器禁用启动器应用程序
pm disable com.android.launcher2
Run Code Online (Sandbox Code Playgroud)
现在Home键按下将始终保持在同一屏幕中.
And*_*lva 13
此解决方案仅适用于3.x.
好吧,这应该是一个难题.但这是破解它的方法.
在您的Activity中覆盖以下方法,
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Run Code Online (Sandbox Code Playgroud)
现在处理这样的关键事件,
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
将此代码添加到您的MainActivity班级:
Timer timer;
MyTimerTask myTimerTask;
@Override
protected void onResume() {
super.onResume();
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
protected void onPause()
{
if (timer == null) {
myTimerTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTimerTask, 100, 100);
}
super.onPause();
}
private void bringApplicationToFront()
{
KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
if( myKeyManager.inKeyguardRestrictedInputMode())
return;
Log.d("TAG", "====Bringging Application to Front====");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
try
{
pendingIntent.send();
}
catch (PendingIntent.CanceledException e)
{
e.printStackTrace();
}
}
public void onBackPressed() {
// do not call super onBackPressed.
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
bringApplicationToFront();
}
}
Run Code Online (Sandbox Code Playgroud)
这不是"主页"按钮的锁定,但用户无法长时间(超过100毫秒)切换到另一个应用程序,也许这就是你想要的.
| 归档时间: |
|
| 查看次数: |
78948 次 |
| 最近记录: |