android系统旁边有一个锁,Android中系统自带锁WalkLock与KeyguardLock用法实例详解

本文实例讲述了Android中系统自带锁WalkLock与KeyguardLock用法。分享给大家供大家参考,具体如下:

WalkLock - 顾名思义 唤醒锁 点亮屏幕用的

KeyguardLock - 顾名思义 键盘锁 解锁键盘用的

详细介绍:

1: WalkLock 唤醒锁

- WalkLock真的能点亮屏幕吗?

答案是肯定的。 可是有时候为什么不点亮屏幕,这个就是参数设置的问题了。

PowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Gank");

PowerManager.FULL_WAKE_LOCK 这个参数是手机点亮的程度,(什么Cpu,屏幕亮度,键盘灯)

PowerManager.ACQUIRE_CAUSES_WAKEUP 关键是这个参数的理解。

WalkLock点亮屏幕并非真的去点亮了屏幕,你可以理解为,它通过Android组件(Activity)去点亮了屏幕。

假如一个通知想去点亮屏幕,问题来了,它能点亮吗? 肯定不行。

不过拥有这个PowerManager.ACQUIRE_CAUSES_WAKEU参数,你就可以点亮屏幕了。它使WalkLock不再依赖组件就可以点亮屏幕了。

- WalkLock如何获得屏幕的状态?

PowerManager.isScreenOn()方法;这个方法返回true: 屏幕是唤醒的  返回false:屏幕是休眠的

- WalkLock唤醒和休眠的方法?

WalkLock.aquire() 在屏幕休眠的状态下唤醒屏幕

WalkLock.release() 在屏幕点亮的状态下,使屏幕休眠。

WalkLock.release()这个方法有个需要注意的地方:

例如:WalkLockA对象先唤醒了屏幕再使屏幕休眠,ok没问题

屏幕本身就是唤醒状态,WalkLockA对象没有唤醒过屏幕,WalkLockA对象如果尝试使屏幕休眠。会产生一个异常 UnLock Sreen。

2:KeyguardLock 键盘锁

- KeyguardLock获得当前屏幕是否解锁?

KeygroundManager.inKeyguardRestrictedInputMode() 返回true表示键盘锁住, 返回false表示键盘解锁中

- KeyguardLock给屏幕解锁和上锁?

KeyguardLock.disableKeyguard()解锁键盘

KeyguardLock.reenableKeyguard()锁键盘

KeyguardLock没有上面唤醒锁的问题,就是无论你键盘是否由KeyguardLockA解锁,你调用KeyguardLockA对象的reenableKeyguard()方法都不会有异常。

这两把锁一些概念性的理解,假如你认为你获得了一个键盘锁对象,你就可以锁屏幕了。这个就大错特错了。

你锁不了其他程序打开的屏幕(如果可以的话,一个for循环一直锁你屏幕,你哭都没眼泪)

你可以控制自己的锁,别想着别人的锁。

最后总结下用法:

一般这两把锁都是配合使用的,你解锁屏幕的时候肯定不希望屏幕漆黑一片。关闭键盘锁的时候希望屏幕也同时休眠。

问题:

1:我尝试手动关闭屏幕,可是总继续亮那么一小会。

2:如果手机自动关闭屏幕的话,不会有这个问题。

public void unlockScreen() {

// 获取PowerManager的实例

PowerManager pm = (PowerManager) mContext

.getSystemService(Context.POWER_SERVICE);

// 得到一个WakeLock唤醒锁

mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK

| PowerManager.ACQUIRE_CAUSES_WAKEUP

| PowerManager.ON_AFTER_RELEASE, "SimpleTimer");

if (!mWakelock.isHeld()) {

// 唤醒屏幕

mWakelock.acquire();

}

// 获得一个KeyguardManager的实例

km = (KeyguardManager) mContext

.getSystemService(Context.KEYGUARD_SERVICE);

// 得到一个键盘锁KeyguardLock

mKeyguardLock = km.newKeyguardLock("SimpleTimer");

if (km.inKeyguardRestrictedInputMode()) {

// 解锁键盘

mKeyguardLock.disableKeyguard();

}

}

注意,这里使用的mWakelock.isHeld())来判断屏幕当前是否是休眠状态,从Android 2.1 API Level7开始增加了一个判断屏幕是否处于点亮状态可以使用public boolean isScreenOn ()这个方法。

锁屏幕的代码是

public void lockScreen() {

// release screen

if (!km.inKeyguardRestrictedInputMode()) {

// 锁键盘

mKeyguardLock.reenableKeyguard();

}

// 使屏幕休眠

if (mWakelock.isHeld()) {

mWakelock.release();

}

}

希望本文所述对大家Android程序设计有所帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android学生信息管理系统实例的代码: 首先,我们需要创建一个MainActivity来作为应用程序的主界面。在该Activity,我们可以添加按钮来实现添加、删除、修改和查询学生信息的功能。 ```java public class MainActivity extends AppCompatActivity { private Button addBtn, deleteBtn, updateBtn, queryBtn; private EditText nameEt, ageEt, phoneEt, emailEt; private ListView studentListView; private SQLiteDatabase db; private StudentAdapter studentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addBtn = findViewById(R.id.add_btn); deleteBtn = findViewById(R.id.delete_btn); updateBtn = findViewById(R.id.update_btn); queryBtn = findViewById(R.id.query_btn); nameEt = findViewById(R.id.name_et); ageEt = findViewById(R.id.age_et); phoneEt = findViewById(R.id.phone_et); emailEt = findViewById(R.id.email_et); studentListView = findViewById(R.id.student_list_view); // 创建或打开数据库 db = openOrCreateDatabase("student.db", MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS student(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phone TEXT, email TEXT)"); // 设置ListView的适配器 studentAdapter = new StudentAdapter(this); studentListView.setAdapter(studentAdapter); // 添加按钮点击事件 addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); int age = Integer.parseInt(ageEt.getText().toString()); String phone = phoneEt.getText().toString(); String email = emailEt.getText().toString(); ContentValues cv = new ContentValues(); cv.put("name", name); cv.put("age", age); cv.put("phone", phone); cv.put("email", email); db.insert("student", null, cv); studentAdapter.notifyDataSetChanged(); } }); // 删除按钮点击事件 deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); db.delete("student", "name=?", new String[]{name}); studentAdapter.notifyDataSetChanged(); } }); // 修改按钮点击事件 updateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); int age = Integer.parseInt(ageEt.getText().toString()); String phone = phoneEt.getText().toString(); String email = emailEt.getText().toString(); ContentValues cv = new ContentValues(); cv.put("age", age); cv.put("phone", phone); cv.put("email", email); db.update("student", cv, "name=?", new String[]{name}); studentAdapter.notifyDataSetChanged(); } }); // 查询按钮点击事件 queryBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); Cursor cursor = db.query("student", null, "name=?", new String[]{name}, null, null, "_id DESC"); if (cursor.moveToFirst()) { int age = cursor.getInt(cursor.getColumnIndex("age")); String phone = cursor.getString(cursor.getColumnIndex("phone")); String email = cursor.getString(cursor.getColumnIndex("email")); ageEt.setText(age + ""); phoneEt.setText(phone); emailEt.setText(email); } else { ageEt.setText(""); phoneEt.setText(""); emailEt.setText(""); Toast.makeText(MainActivity.this, "没有找到该学生信息", Toast.LENGTH_SHORT).show(); } cursor.close(); } }); } // 自定义ListView的适配器 private class StudentAdapter extends BaseAdapter { private Context context; private Cursor cursor; public StudentAdapter(Context context) { this.context = context; cursor = db.query("student", null, null, null, null, null, "_id DESC"); } @Override public int getCount() { return cursor.getCount(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.student_list_item, null); } TextView nameTv = convertView.findViewById(R.id.name_tv); TextView ageTv = convertView.findViewById(R.id.age_tv); TextView phoneTv = convertView.findViewById(R.id.phone_tv); TextView emailTv = convertView.findViewById(R.id.email_tv); cursor.moveToPosition(position); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); String phone = cursor.getString(cursor.getColumnIndex("phone")); String email = cursor.getString(cursor.getColumnIndex("email")); nameTv.setText(name); ageTv.setText(age + ""); phoneTv.setText(phone); emailTv.setText(email); return convertView; } } } ``` 在布局文件,我们需要添加一个ListView和一些EditText和Button来实现添加、删除、修改和查询学生信息的功能。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <EditText android:id="@+id/name_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="姓名" /> <EditText android:id="@+id/age_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="年龄" /> <EditText android:id="@+id/phone_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="电话" /> <EditText android:id="@+id/email_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="邮箱" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name_et" android:layout_marginTop="16dp" android:layout_centerHorizontal="true" android:orientation="horizontal"> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/update_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/query_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" /> </LinearLayout> <ListView android:id="@+id/student_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/add_btn" /> </RelativeLayout> ``` 最后,我们需要创建一个用于显示每个学生信息的ListView的适配器StudentAdapter。在该适配器,我们需要从数据库读取学生信息,然后将它们显示在ListView。 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:id="@+id/name_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="姓名" android:textSize="16sp" /> <TextView android:id="@+id/age_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="年龄" android:textSize="16sp" /> <TextView android:id="@+id/phone_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="电话" android:textSize="16sp" /> <TextView android:id="@+id/email_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="邮箱" android:textSize="16sp" /> </LinearLayout> ``` 这样,我们就完成了一个简单的Android学生信息管理系统实例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值