努力以编程方式管理app锁

时间:2017-01-03 11:52:32

标签: java android

下面的代码列出了我的Android设备上安装的应用包 - 它们的名称和图标显示在ListView中:

PackageInfo packageInfo = (PackageInfo) getItem(position);
        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 40, 40);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);

我正在尝试实施一项安全功能,以便当我使用“开/关”按钮切换应用时功能,到' on'状态,我希望手机在让应用程序启动之前用锁定屏幕进行调解,否则应用程序应该正常启动。

但是,我在遇到这种行为时遇到了麻烦,我很感激一些建议。这是代码:

public class MyService  extends Service {
    private static Timer timer = new Timer();
    public Boolean userAuth = false;
    private Context ctx;
    public String pActivity="";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    public void onCreate()
    {
        super.onCreate();
        ctx = this;
        startService();
    }


    private void startService()
    {
        timer.scheduleAtFixedRate(new mainTask(), 0, 500);
    }

    private class mainTask extends TimerTask
    {
        public void run()
        {
            toastHandler.sendEmptyMessage(0);
        }
    }



//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        // Let it continue running until it is stopped.
//        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
//        return START_STICKY;
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
    }


    private final Handler toastHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            String activityOnTop;
            ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
            ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
            activityOnTop=ar.topActivity.getClassName();

            if(activityOnTop.equals("app.example.pnf.new_app.MainActivity"))
            {
                pActivity = activityOnTop.toString();
                Toast.makeText(MyService.this, "hello0"+pActivity, Toast.LENGTH_SHORT).show();

            }
            else if(activityOnTop.equals(pActivity) || activityOnTop.equals("app.example.pnf.new_app.MainActivity"))
                {
                    Toast.makeText(MyService.this, "hello1"+pActivity, Toast.LENGTH_SHORT).show();

                }
                else
                {

                    Intent i = new Intent(MyService.this, LockScreen.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                    Toast.makeText(MyService.this, pActivity,Toast.LENGTH_LONG).show();
                    pActivity = activityOnTop.toString();
                }
           }
    };
}

0 个答案:

没有答案