我目前正在使用自定义AlertDialog,它在Activity中有一个Theme.Dialog主题,要求用户输入当前密码才能继续。
根据Android - Is It possible to disable the click of home button我尝试了大多数方法,但它不起作用。
活动 - onCreate
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(PhysicalTheftDialog.this);
boolean isServerRunning = sp.getBoolean("isServerRunning", false);
if (isServerRunning == false) {
startService(new Intent(PhysicalTheftDialog.this, MyService.class));
}
else {
sp.getBoolean("isServerRunning", true);
}
服务
@Override
public void onCreate() {
/* //Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setOnPreparedListener(this);
try {
player.prepareAsync();
} catch (Exception e) {
Log.e(TAG, "", e);
}
player.setLooping(true); // Set looping*/
this.context = getApplicationContext();
//Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isServerRunning = sp.getBoolean("isServerRunning", true);
while (!isServerRunning) {
// THAT CODE: check if activity is on the top
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// get a list of running processes and iterate through them
ActivityManager activityManager = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
// if top activity is not "MyActivity" activity, then launch it
if("nyp.android.project".equals(componentInfo.getClassName()) == false) {
Intent intent = new Intent(MyService.this, PhysicalTheftDialog.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}, 2000);
boolean serviceStatus = sp.getBoolean("isServerRunning", false);
}
}
logcat的
E/AndroidRuntime( 9799): FATAL EXCEPTION: main
E/AndroidRuntime( 9799): java.lang.RuntimeException: Unable to start service nyp.android.project.MyService@41b29ff0 with Intent { cmp=xxxx }: java.lang.NullPointerException
E/AndroidRuntime( 9799): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2387)
E/AndroidRuntime( 9799): at android.app.ActivityThread.access$1900(ActivityThread.java:127)
E/AndroidRuntime( 9799): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
E/AndroidRuntime( 9799): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 9799): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 9799): at android.app.ActivityThread.main(ActivityThread.java:4511)
E/AndroidRuntime( 9799): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9799): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 9799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
E/AndroidRuntime( 9799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
E/AndroidRuntime( 9799): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 9799): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9799): at nyp.android.project.MyService.onStart(MyService.java:95)
E/AndroidRuntime( 9799): at android.app.Service.onStartCommand(Service.java:438)
E/AndroidRuntime( 9799): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2370)
E/AndroidRuntime( 9799): ... 10 more
答案 0 :(得分:1)
McAfee Wave Secure通过以下方式完成此操作:它们运行后台服务,如果您的屏幕处于活动状态,则每隔几秒检查一次。如果不是服务重新启动您的活动或将其带到前面(使其激活)。在onPause中,您可以保存输入数据,以便在重新启动活动时重新加载它。
我的例子:
// get a list of running processes and iterate through them
ActivityManager activityManager = (ActivityManager)_context
.getSystemService(Activity.ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
// if top activity is not "MyActivity" activity, then launch it
if(componentInfo.getClassName().equals("com.namespace.MyActivity") == false) {
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
我做了“FLAG_ACTIVITY_NEW_TASK”,因为我已经获得了相同活动的许多实例。也许有办法让它变得活跃。
示例“PSEUDO”
{
Activity:
preference isServerRunning = getPreference("isServerRunning");
if (serviceIsRunning == false) {
runService();
}
// othervise, service is running and will do the job
Onclick.UserLogged() {
setPreference("isUserLoged", true);
}
Service:
while (!userIsLogged) {
// THAT CODE: check if activity is on the top
sleepFor2seconds();
userIsLogger = getPreference("isUserLogged");
}
}
答案 1 :(得分:0)
尝试覆盖onPause()方法,以便当按下主页按钮时活动不会停止
答案 2 :(得分:0)
尝试覆盖您希望禁用HOME按钮的Activity类的onKeyDown方法。在此方法中,检查用户是否按下了HOME按钮。