Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓锁应用程序_Android - Fatal编程技术网

Android 安卓锁应用程序

Android 安卓锁应用程序,android,Android,我正在尝试制作一个应用程序,用密码锁定所有应用程序,因为“Kiosk模式”无法实现,正如你在我的问题中看到的() 因此,我做的是做一个后台服务,检查应用程序是否已打开,并启动一个请求密码的新活动(我还没有实现密码,可以不检查密码就完成,这不是我的问题) 问题是,当我打开应用程序时,我的服务没有运行,锁屏活动也没有显示 我用它来写我的后台服务(@Amit Gupta answer)和(@Muhammad Zeeshan Karamat answer) 我找到了这个答案,但对我没有帮助 以下是我的一

我正在尝试制作一个应用程序,用密码锁定所有应用程序,因为“Kiosk模式”无法实现,正如你在我的问题中看到的()

因此,我做的是做一个后台服务,检查应用程序是否已打开,并启动一个请求密码的新活动(我还没有实现密码,可以不检查密码就完成,这不是我的问题)

问题是,当我打开应用程序时,我的服务没有运行,锁屏活动也没有显示

我用它来写我的后台服务(@Amit Gupta answer)和(@Muhammad Zeeshan Karamat answer)

我找到了这个答案,但对我没有帮助

以下是我的一些代码:

启动服务的主要活动:

 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);
}
编辑: 当我返回START\u STICKY时,onstart命令应该可以工作;但它也不起作用。 我们将不胜感激


谢谢

首先,如果您的服务还没有启动,那么请登录
AndroidManifest.xml

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

我的服务开始了,我添加了代码,但现在即使我在主页上也会出现垃圾邮件锁屏。是的,它每5秒钟工作一次,你应该在锁屏启动后取消倒计时。类,使用这个。取消()。。我刚刚告诉你当你打开任何其他应用程序(包括Launcher)时启动LockScreen.class的初始逻辑,根据需要修改:)是的,但是如果我在LockScreen.class打开时取消倒计时,那么我可以从通知栏打开设置并跳过密码检查。(我可以从设置中删除应用程序)我想如果我能知道另一个活动何时打开,然后打开锁屏比打开计时器要好得多。不确切地说,如果你想锁定facebook和whatsapp,那么每次都要检查它们中是否有一个在顶级活动中。例如,如果whatsapp在顶级活动中,那么打开锁屏类&存储你锁定的whatsapp&continue计时器,&确保在用户重新打开whatsapp之前不会再次锁定它。可能有更好的方法,检查是否有任何开源应用程序锁。这只是我的2美分:)最好的luck@sam_0829你们能告诉我我将如何解决这个问题吗?我已经发布了你们之前关于禁用通知栏下拉的问题的答案。检查一下。@AmitGupta你能检查一下吗please@Hamza您如何解决您的问题?请您分享您的知识每2秒启动一次服务将耗尽电池,这将是appI的一个缺点。我暂时搁置,尚未解决
<service android:name=".MyService" android:enabled="true" />
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();
           }
};
check.start();