Java 需要一个新的解决方案来禁用android中的主菜单按钮吗
我找了很多东西,几乎什么都试过了,但什么都不管用。 我只想禁用主页和菜单按钮 下面是LockScreenActivity.javaJava 需要一个新的解决方案来禁用android中的主菜单按钮吗,java,android,button,android-homebutton,Java,Android,Button,Android Homebutton,我找了很多东西,几乎什么都试过了,但什么都不管用。 我只想禁用主页和菜单按钮 下面是LockScreenActivity.java import android.app.ActionBar; import android.app.Activity; import android.app.KeyguardManager; import android.content.Intent; import android.os.Build; import android.os.Bundle; import
import android.app.ActionBar;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.mehuljoisar.lockscreen.utils.LockscreenService;
import com.mehuljoisar.lockscreen.utils.LockscreenUtils;
public class LockScreenActivity extends Activity implements
LockscreenUtils.OnLockStatusChangedListener {
// User-interface
private Button btnUnlock;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private ImageButton button10;
private TextView textView;
private RelativeLayout myView;
// Member variables
private LockscreenUtils mLockscreenUtils;
// Set appropriate flags to make the screen appear over the keyguard
@Override
public void onAttachedToWindow() {
this.getWindow().setType(
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
this.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
);
super.onAttachedToWindow();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_lockscreen);
init();
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
button5=(Button)findViewById(R.id.button5);
button6=(Button)findViewById(R.id.button6);
button7=(Button)findViewById(R.id.button7);
button8=(Button)findViewById(R.id.button8);
button9=(Button)findViewById(R.id.button9);
button10=(ImageButton)findViewById(R.id.button10);
textView=(TextView)findViewById(R.id.textView1);
myView=(RelativeLayout)findViewById(R.id.layout);
// Ask the System Bar to hide
myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"8");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"5");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"2");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"7");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"4");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"9");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"3");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText(textView.getText().toString()+"6");
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(textView.getText().toString().length()>0)
// TODO Auto-generated method stub
textView.setText(textView.getText().toString().substring(0, textView.getText().toString().length()-1));
}
});
// unlock screen in case of app get killed by system
if (getIntent() != null && getIntent().hasExtra("kill")
&& getIntent().getExtras().getInt("kill") == 1) {
enableKeyguard();
unlockHomeButton();
} else {
try {
// disable keyguard
disableKeyguard();
// lock home button
lockHomeButton();
// start service for observing intents
startService(new Intent(this, LockscreenService.class));
// listen the events get fired during the call
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
}
}
}
private void init() {
mLockscreenUtils = new LockscreenUtils();
btnUnlock = (Button) findViewById(R.id.btnUnlock);
btnUnlock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// unlock home button and then screen on button press
if(textView.getText().toString().equals("1234"))
unlockHomeButton();
}
});
}
// Handle events of calls and unlock screen if necessary
private class StateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
unlockHomeButton();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
// Don't finish Activity on Back press
@Override
public void onBackPressed() {
return;
}
// Handle button clicks
@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_POWER)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|| (keyCode == KeyEvent.KEYCODE_CAMERA)) {
return true;
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
return false;
}
return true;
}
// handle the key press events here itself
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
|| (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
return false;
}
if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {
return false;
}
return false;
}
// Lock home button
public void lockHomeButton() {
mLockscreenUtils.lock(LockScreenActivity.this);
}
// Unlock home button and wait for its callback
public void unlockHomeButton() {
mLockscreenUtils.unlock();
}
// Simply unlock device when home button is successfully unlocked
@Override
public void onLockStatusChanged(boolean isLocked) {
if (!isLocked) {
unlockDevice();
}
}
@Override
protected void onStop() {
super.onStop();
unlockHomeButton();
}
@SuppressWarnings("deprecation")
private void disableKeyguard() {
KeyguardManager mKM = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mKL = mKM.newKeyguardLock("IN");
mKL.disableKeyguard();
}
@SuppressWarnings("deprecation")
private void enableKeyguard() {
KeyguardManager mKM = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mKL = mKM.newKeyguardLock("IN");
mKL.reenableKeyguard();
}
//Simply unlock device by finishing the activity
private void unlockDevice()
{
finish();
}
}
这是我的LockscreenIntentReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import com.mehuljoisar.lockscreen.LockScreenActivity;
import com.mehuljoisar.lockscreen.R;
public class LockscreenIntentReceiver extends BroadcastReceiver {
// Handle actions and display Lockscreen
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
|| intent.getAction().equals(Intent.ACTION_SCREEN_ON)
|| intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
start_lockscreen(context);
}
}
// Display lock screen
private void start_lockscreen(Context context) {
Intent mIntent = new Intent(context, LockScreenActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
这是我的LockscreenService.java
import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.provider.SyncStateContract.Constants;
import android.support.v4.app.NotificationCompat;
import com.mehuljoisar.lockscreen.R;
public class LockscreenService extends Service {
private BroadcastReceiver mReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
// Register for Lockscreen event intents
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new LockscreenIntentReceiver();
registerReceiver(mReceiver, filter);
startForeground();
return START_STICKY;
}
// Run service in foreground so it is less likely to be killed by system
private void startForeground() {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setTicker(getResources().getString(R.string.app_name))
.setContentText("Running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(null)
.setOngoing(true)
.build();
startForeground(9999,notification);
}
// Unregister receiver
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
这是我的LockscreenUtils.java
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.RelativeLayout;
import com.mehuljoisar.lockscreen.R;
public class LockscreenUtils {
// Member variables
private OverlayDialog mOverlayDialog;
private OnLockStatusChangedListener mLockStatusChangedListener;
// Interface to communicate with owner activity
public interface OnLockStatusChangedListener
{
public void onLockStatusChanged(boolean isLocked);
}
// Reset the variables
public LockscreenUtils() {
reset();
}
// Display overlay dialog with a view to prevent home button click
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
mLockStatusChangedListener = (OnLockStatusChangedListener) activity;
}
}
// Reset variables
public void reset() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
// Unlock the home button and give callback to unlock the screen
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
if(mLockStatusChangedListener!=null)
{
mLockStatusChangedListener.onLockStatusChanged(false);
}
}
}
// Create overlay dialog for lockedscreen to disable hardware buttons
private static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
getActionBar().setDisplayHomeAsUpEnabled(false);
params.type = LayoutParams.TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F;
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_NOT_TOUCH_MODAL,
0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
// consume touch events
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
}
}
最后但并非最不重要。。这是我的舱单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mehuljoisar.lockscreen"
android:versionCode="3"
android:versionName="1.2" >
<uses-sdk android:minSdkVersion="11" />
<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" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.mehuljoisar.lockscreen.LockScreenActivity"
android:excludeFromRecents="true"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".utils.LockscreenService" >
</service>
<receiver
android:name=".utils.LockscreenIntentReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
使用以下方法隐藏主/菜单按钮:
getActionBar().setDisplayHomeAsUpEnabled(false)
或使用
getActionBar().setHomeButtonEnabled (false)
禁用与主/向上提供的交互。您是否尝试过
getSupportActionBar()。setDisplayHomeAsUpEnabled(false)代码>?我已经在LockScreenUtils中添加了它。java类,但当我将其添加到LockScreenActivity.java中的onCreate方法中时,应用程序崩溃并且无法打开。现在,我尝试了eclipse的快速解决方案,它添加了这个@SuppressLint(“NewApi”)@覆盖,以便我能够编写getActionBar()。setHomeButtonEnabled(false)home按钮仍然处于启用状态–收到一个错误,说调用需要min sdk“14”,当我将minSdk更改为14时,我的应用程序没有启动,它崩溃了@Kishudroidw你的日志猫说什么?在清单中添加安卓:主题=“@android:style/theme.Holo.Light”。请查看我的日志猫说了什么。我补充说,只有当我将MinSDK从11更改为14时,才会发生这种情况