Android锁定应用程序

时间:2014-08-25 07:59:36

标签: android

我试图创建一个用密码锁定所有应用程序的应用程序,因为" Kiosk模式"你可以在我的问题(disabling and hiding android navigation bar/notification menu permanently

中看到

所以我做的是我做一个后台服务,检查一个应用程序是否已经打开,我开始一个请求密码的新活动(我还没有实现密码,它可以在不检查密码的情况下完成密码,这不是我的问题)。

问题是我的服务没有运行,而且当我打开一个应用程序时,我的LockScreen活动没有显示。

我用它来写我的后台服务(Blocking android apps programmatically @Amit Gupta回答)和(Using Service to run background and create notification @Muhammad Zeeshan Karamat答案)

我找到了这个答案,但它没有帮助我(How to always run a service in the background?

以下是我的一些代码:

启动服务的主要活动:

 public class LockAppsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);
             button.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     startService(new Intent(LockAppsActivity.this, MyService.class));

                 }
             });

    }...etc

我的服务类:

public class MyService extends Service{
@Override
public IBinder onBind(Intent arg0) {
    return null;
}


@Override
public void onCreate() {
    Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    PackageManager packageManager = getPackageManager();
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
    Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
    List<PackageInfo> packs = packageManager.getInstalledPackages(0);
    for(int i=0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        ApplicationInfo a = p.applicationInfo;
        // skip system apps if they shall not be included
        //apps.add(p.packageName);
    }

    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
    ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
    String activityOnTop = ar.topActivity.getClassName();


    if(!activityOnTop.equals("com.example.lock")){
        Intent lockIntent = new Intent(this, LockScreen.class);
        lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(lockIntent);
    }
    Toast.makeText(this, "My Service Running", Toast.LENGTH_LONG).show();
    return super.onStartCommand(intent, flags, startId);
}

这是我的LockScreen类,当我启动任何其他应用程序时它没有显示:

public class LockScreen extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


    Button button = (Button) findViewById(R.id.okButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            LockScreen.this.onBackPressed();

        }
    });


    Button cancelButton = (Button) findViewById(R.id.cancelButton);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent startHomescreen=new Intent(Intent.ACTION_MAIN);
            startHomescreen.addCategory(Intent.CATEGORY_HOME);
            startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(startHomescreen);
        }
    });


}//onCreate

编辑: 当我返回START_STICKY时,onStartCommand应该工作;但它也不起作用。 帮助将不胜感激,

由于

1 个答案:

答案 0 :(得分:0)

首先,如果您的服务甚至没有启动,请检查AndroidManifest.xml以获取此

<service android:name=".MyService" android:enabled="true" />

如果从我的理解开始,那么您正在检查服务启动时正在运行的应用程序。

目前,只是为了测试你的代码,实现一个CountDownTimer,它将每隔5秒检查一次TOP活动

Protected CountDownTimer check = new CountDownTimer(5000, 5000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                //Do nothing here

            }

            @Override
            public void onFinish()
            {
                PackageManager packageManager = getPackageManager();
                Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
                Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
                List<PackageInfo> packs = packageManager.getInstalledPackages(0);
                for(int i=0; i < packs.size(); i++) 
                {
                    PackageInfo p = packs.get(i);
                    ApplicationInfo a = p.applicationInfo;
                    // skip system apps if they shall not be included
                    //apps.add(p.packageName);
                }   

                ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
                ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
                String activityOnTop = ar.topActivity.getClassName();


                if(!activityOnTop.equals("com.example.lock"))
                {
                    Intent lockIntent = new Intent(this, LockScreen.class);
                    lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(lockIntent);
                }
                this.cancel();
                this.start();
           }
};

不要忘记从onCreate of service

开始倒数计时器
check.start();