如何禁用电源按钮长按事件在孩子们的地方Android应用程序

时间:2016-11-29 12:41:22

标签: android android-studio gridview kiosk-mode

我正在为孩子们开发一个应用程序,其中我已经显示了所有应用程序的网格视图,这些应用程序由父母在孩子们的地方添加并具有适当的锁定功能,因此孩子无法退出应用程序。我成功地做到了这一切。但是我的一个锁定功能是电源按钮长按是有问题的。它在主要活动中正常工作,我在gridView中列出了所有添加的应用程序。但是当用户点击gridView中的任何应用程序项目时,我的长动力按下事件不起作用。当孩子按住长按电源按钮时,我不想显示系统对话框。我搜索了很多,尝试了很多东西,但没有任何作用。我使用this作为参考来源 以下是我的代码: (各种帮助表示赞赏)

mainactivity.java:

public class Teen4Activity extends Activity implements AdapterView.OnItemClickListener
{
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private AppInfoAdapter listadaptor = null;
    GridView gridview;
    private Button hiddenExitButton;

    dbHelper myDb;
    ArrayList<ApplicationInfo> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); //for short power press

        //for disabling status bar
        WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        localLayoutParams.gravity = Gravity.TOP;
        localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources().getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;
        customViewGroup view = new customViewGroup(this);
        manager.addView(view, localLayoutParams);

        setContentView(R.layout.activity_teen4);
        // every time someone enters the kiosk mode, set the flag true
        PrefUtils.setKioskModeActive(true, getApplicationContext());
        Toast.makeText(getApplicationContext(), "Device Locked!", Toast.LENGTH_LONG).show();

        gridview = (GridView) findViewById(R.id.gridview);
        hiddenExitButton = (Button) findViewById(R.id.button5);
        hiddenExitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Break out!
                PrefUtils.setKioskModeActive(false, getApplicationContext());
                Toast.makeText(getApplicationContext(),"You can leave the app now!", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        myDb=new dbHelper(this);  // getting the added apps from database
        ArrayList<String> columnArray1 = new ArrayList<String>();
        Cursor res=myDb.getAllData();
        if(res.getCount()==0)
        {
            //showing error message for no data
            showMessage("Error","First Add Apps to Teens Place");
            return;
        }
        StringBuffer buffer=new StringBuffer();
        while(res.moveToNext())
        {
            buffer.append("APPS :"+res.getString(0)+"\n");
            columnArray1.add(res.getString(0));
        }
        //showMessage("Data",buffer.toString());
        Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC).serializeNulls().create();
        String json =columnArray1.toString() ;
        Type type = new TypeToken<ArrayList<ApplicationInfo>>() {}.getType();
        arrayList = gson.fromJson(json, type);

        packageManager = getPackageManager();
        new LoadApplications().execute();
    }

    @Override
    public void onBackPressed() {
        // nothing to do here
        // … really
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!hasFocus) {
            // Close every kind of system dialog
            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected Void doInBackground(Void... params)
        {
            applist = arrayList;
            Log.d("list size", String.valueOf(applist.size()));
            listadaptor = new AppInfoAdapter(Teen4Activity.this, R.layout.layout_appinfo, applist);
            System.out.println("adapter="+listadaptor);
            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result)
        {

            gridview.setAdapter(listadaptor);
            gridview.setOnItemClickListener(Teen4Activity.this);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        ApplicationInfo app = applist.get(position);
        try {

            Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

            if (null != intent)
            {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(Teen4Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(Teen4Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return applist;
    }
    public void showMessage(String title,String message)
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
}

AppInfoAdapter :(用于在gridView项目中显示应用的适配器类)

public class AppInfoAdapter extends  ArrayAdapter<ApplicationInfo>
{
    private List<ApplicationInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;

    public AppInfoAdapter(Context context,int textViewResourceId, List<ApplicationInfo> appsList)
    {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.layout_appinfo, null);
        }

        ApplicationInfo data = appsList.get(position);
        if (null != data) {
            TextView appName = (TextView) view.findViewById(R.id.tvName);
            ImageView iconview = (ImageView) view.findViewById(R.id.ivIcon);

            appName.setText(data.loadLabel(packageManager));
            iconview.setImageDrawable(data.loadIcon(packageManager));
        }
        return view;
    }
}

0 个答案:

没有答案