2012-07-25 86 views
1

我目前正在使用一个自定义AlertDialog,它有一个Activity中的Theme.Dialog主题,要求用户键入当前密码才能继续。防止HOME按钮关闭活动

根据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 [email protected] 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

顺便说一句:if(“nyp.android.project”.equals(service.service.getClassName()))是一个Yoda条件http://www.codinghorror.com/blog/2012/07/new-programming- jargon.html – Maxim 2012-07-25 15:48:05

+0

boolean isServerRunning = sp.getBoolean(“isServerRunning”,true);如果在首选项中没有“isServiceRunning”,那么您的代码默认返回true。我认为应该是假的。它看起来没问题。在调试器中运行,看看它是如何工作的。 – Maxim 2012-07-25 19:25:40

+0

我收到'ActivityManager无法解析为变量' – dythe 2012-07-25 19:59:13

回答

0

尝试overiding的的onPause()方法,这样的活动不会停止home键按下呼叫时它

+0

我在onPause()方法中放什么? – dythe 2012-07-25 14:47:20

+0

任何你想要的或没有什么只是不打电话super.onPause()这是实际上暂停活动的代码 – ghostbust555 2012-07-25 14:51:52

0

从我看到的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” 因为I`ve已经得到同样的活动的许多实例。也许有一种方法可以使它活跃起来。

例“伪”

{ 
    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"); 
    } 

} 
+0

任何想法如何检查屏幕是否活动? – dythe 2012-07-25 14:57:56

+0

所以我只是把它放在服务的onCreate()方法中,并在我的主Activity中使用,我将在onPause()方法中调用服务? – dythe 2012-07-25 15:11:26

+0

这可以这样实现:当您的活动运行时,请检查一个标志(例如,有一个首选项),如果您的服务正在运行或不运行。如果没有,则设置标志并运行服务。在工作完成之前(当用户仍然需要做某事时),每2-5秒在一个循环中服务将检查特定活动是否位于活动堆栈顶部,如果不是,则将其置于顶部。当用户完成时,将标志设置为关闭,服务器下一次运行将读取标志并完成其运行 – Maxim 2012-07-25 15:22:10

0

尝试重写的方法OnKeyDown您的活动类,其中u要禁用HOME按钮。在这种方法中,检查用户是否按下了HOME按钮。

+0

onKeyDown不起作用,只适用于BACK按钮 – dythe 2012-07-25 14:52:32