在实现锁屏功能时能可能会出现这个问题出现.....这主要是因为锁屏需要管理员的权限.......
解决方法如下:
1、新建一个MyAdmin.java
package com.njupt.testrotate1;
import android.app.admin.DeviceAdminReceiver;
public class MyAdmin extends DeviceAdminReceiver {
}
2、在清单文件中如下配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.njupt.testrotate1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.njupt.testrotate1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.njupt.testrotate1.MyAdmin" >
<meta-data android:name="android.app.device_admin"
android:resource="@xml/my_admin"/>
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
3、在res目录下新建xml/my_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<!--
limit-password 设置密码的规则
watch-login 监控屏幕解锁尝试次数
reset-password 更改屏幕解锁密码
force-lock 设备自动锁屏
wipe-data 删除全部的数据
-->
<uses-policies>
<limit-password/>
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
4、在MainAcitivity中如下书写:
private DevicePolicyManager devicePolicyManager;
private boolean isAdminActive;
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 申请权限
ComponentName componentName = new ComponentName(this, MyAdmin.class);
// 判断该组件是否有系统管理员的权限
isAdminActive = devicePolicyManager
.isAdminActive(componentName);
if(!isAdminActive){//这一句一定要有...
Intent intent = new Intent();
//指定动作
intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//指定给那个组件授权
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivity(intent);
}
if(isAdminActive){
Toast.makeText(this, "具有权限,将进行锁屏....", 1).show();
devicePolicyManager.lockNow();
devicePolicyManager.resetPassword("123321", 0);
}
MainAcitivity的完整代码如下:
package com.njupt.testrotate1;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private Button clear;
private TextView tv1;
private SensorManager mSensorManager;
private Vibrator vibrator;
private int counter = 1;
private DevicePolicyManager devicePolicyManager;
private boolean isAdminActive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
// clear = (Button) findViewById(R.id.clear);
// clear.setOnClickListener(new Button.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// clear.setText("现在给button赋值喽....");
// }
// });
//
tv1 = (TextView) findViewById(R.id.tv1);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 申请权限
ComponentName componentName = new ComponentName(this, MyAdmin.class);
// 判断该组件是否有系统管理员的权限
isAdminActive = devicePolicyManager
.isAdminActive(componentName);
if(!isAdminActive){
Intent intent = new Intent();
//指定动作
intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//指定给那个组件授权
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
float[] values = event.values;
float x = values[0];
float y = values[1];
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
tv1.setText("现在的x轴是: " + x + " y轴是: " + y);
if (Math.abs(x) > 9.0 || Math.abs(y) > 9.0) {
// Toast.makeText(this, "现在的垂直方向已经超过了90度,将进行锁屏", 1).show();
vibrator.vibrate(500);
System.out.println("...............isAdminActive: "
+ isAdminActive);
if(isAdminActive){
Toast.makeText(this, "具有权限,将进行锁屏....", 1).show();
devicePolicyManager.lockNow();
devicePolicyManager.resetPassword("123321", 0);
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我们知道,在做网络异步请求的时候,有时候需要在收到数据时进行一些界面的更新,为了更简单地与UI主线程交互,我稍微封装了下。
import java.util.HashMap;
import java.util.Map;
import android.os.Handler;
import android.os.Message;
public class HandlerHelper {
public static class MyHandler extends Handler {
private Map<String, HandlerCallback> table_cb = new HashMap<String, HandlerCallback>();
@Override
public void handleMessage(Message msg) {
if(msg.what == 1) {
if(msg.obj != null) {
HandlerCallback _cb = (HandlerCallback)msg.obj;
_cb.callBack();
msg.what = 0;
}
}
super.handleMessage(msg);
}
public void sendMsg(HandlerCallback _cb, Object tag) {
_cb.tag = tag;
Message msg = new Message();
msg.what = 1;
msg.obj = _cb;
this.sendMessage(msg);
}
public void sendMsg(String key, Object tag) {
if(table_cb.containsKey(key)) {
HandlerCallback cb = table_cb.get(key);
cb.tag = tag;
Message msg = new Message();
msg.what = 1;
msg.obj = cb;
this.sendMessage(msg);
}
}
public void setHandlerCallback(String key, HandlerCallback _cb) {
table_cb.put(key, _cb);
}
}
public abstract static class HandlerCallback {
public Object tag;
public abstract void callBack();
}
}
tag是为了传递额外数据给UI主线程,具体用法:
private MyHandler mhandler = new MyHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
mhandler.setHandlerCallback("music_status_refresh", new HandlerCallback() {
@Override
public void callBack() {
NotificationHelper.updateNotification();
adapter_songlist.notifyDataSetChanged();
if(pd.play_status == PublicData.PLAYSTATE_PLAYING) {
aq.id(R.id.amain_bottom_bar_title).text("正在播放:" + pd.song_nowpalying.name);
aq.id(R.id.amain_bottom_bar_info).text(pd.song_nowpalying.artist_name);
aq.id(R.id.amain_bottom_bar_icon).image(pd.song_nowpalying.album_logo, true, true, 0, 0);
aq.id(R.id.amain_bottom_bar).visible();
} else {
aq.id(R.id.amain_bottom_bar).gone();
}
}
});
mhandler.setHandlerCallback("adapter_notify", new HandlerCallback() {
@Override
public void callBack() {
adapter_songlist.notifyDataSetChanged();
}
});
pd.list_service_callback.add(mhandler);
setupView();
is_songlist_getting = true;
myapi.getSongList(list_song, list_now_type, next_page);
if(pd.setting_check_updata) {
UpdataHelper.checkUpdata(aq, false);
}
}
然后在网络异步回调时(mhandler_amain为上面的mhandler):
public void downloadSong(final Song song, final MyHandler handler) {
//...
aq.progress(new MyOnProgressListener(song)).download(song.location, target, new AjaxCallback<File>() {
@Override
public void callback(String url, final File file, AjaxStatus status) {
mhandler_amain.sendMsg(new HandlerCallback() {
@Override
public void callBack() {
NotificationHelper.updateNotification(Integer.valueOf(song.song_id),
"下载 " + song.name + " 完成", file.getAbsolutePath(), true);
song.local_location = getSongLocalLocation(song);
song.is_local = true;
if(handler != null)
handler.sendMsg("adapter_notify", null);
Toast.makeText(aq.getContext(), aq.getContext().getString(R.string.toast_download_finished),
Toast.LENGTH_SHORT).show();
}
},null);
}
});
}
【课程内容】
今天我们将介绍用户交互技术--屏幕拾取技术,并设计控制中心类,实现更复杂的控制逻辑。
【源代码下载地址】http://download.csdn.net/detail/elong_2009/6455097
前面几天的课程,我们实验了几种渲染的技术,这些技术是后续开发的基础。今天我们将研究用户交互的技术,以实现与用户的互动。
1、屏幕拾取技术
在OpenGL ES的开发环境下,可以利用的屏幕拾取技术有很多种,如颜色拾取、射线相交等。但是这些技术对于我们正在山寨的应用来说,显得过于复杂了。基于应用特定的需求,我们不需要使用复杂的技术就能够完成屏幕拾取。
从技术角度来说,简单就是美,能够满足需求就是好的。对于基于棋盘布局的消除游戏,每个可触碰对象的坐标范围是可以预测的,因此我们决定采用最简单的屏幕坐标定位技术。
对于更复杂的场景,如对象坐标是随时改变或不能简单预测的,或者对象表面是不规则形状的,需要采用其它更复杂的屏幕拾取技术。如果有缘,你们会在笔者另外一个作品《教你玩魔方》中看到这种技术的介绍,合适的时候,我会向大家详细介绍这种技术。目前这种技术在网络上可参考的示例还不是太多。
2、ScreenTouch 类
我们设计了屏幕拾取ScreenTouch 类来获取用户正在操作的对象,如果用户的操作是有效的,将通过RaiseTouchEvent 方法产生一个事件,用消息的方式通知控制中心ControlCenter处理该动作。
//产生有效的触摸事件,发消息给mHandler统一处理
void RaiseTouchEvent()
{
if(!isValidTouch()) //校验动作是否合法
return;
Toast.makeText(mContext, "Direction:" + getDirection() + " (" + getGridX() + " ," + getGridY() + ")",
Toast.LENGTH_SHORT).show();
Bundle b = new Bundle();
b.putInt("col1", getGridX());
b.putInt("row1", getGridY());
b.putInt("col2", getNeighborX());
b.putInt("row2", getNeighborY());
Message msg = new Message();
msg.what = ControlCenter.EXCHANGE_START;
msg.setData(b);
ControlCenter.mHandler.sendMessage(msg);
}
每个touch事件必定会触发一个交换动作,因此我们向mHandler发送的消息类型定义为EXCHANGE_START。
ScreenTouch 类提供了一个公有的方法供CrazyLinkGLSurfaceView.onTouchEvent()方法调用
public boolean Touch(MotionEvent e);
3、控制中心ControlCenter 类设计
到目前为止,我们已经有了若干个渲染类DrawXxx,渲染动态控制效果类CtlXxx以及用户交互类ScreenTouch。现在,需要把这些独立的功能组合起来。
从本节开始,我们开始设计游戏的逻辑算法类ControlCenter ,该类包含在 package elong.CrazyLink.Core中,后续各种游戏逻辑控制算法,都会放在这个包下面。
我们将前面课程的代码进行了少量的修改,以便适合新逻辑。
(1)将纹理操作、渲染对象、渲染动态控制对象等从CrazyLinkGLSurfaceView 移除,取而代之是一个static ControlCenter controlCenter;对象
(2)将纹理操作、渲染对象、渲染动态控制对象等移到ControlCenter 类中。
(3)DrawLoading、DrawExchang 对象根据特定事件动作来创建,不需要在初始化时就创建好。
(4)增加了消息处理的机制。mHandler.handleMessage负责处理各种消息类型。
下面给出了消息处理的代码,后续更负责的处理逻辑,大都是通过增加对应的消息处理来实现的:
//消息处理
public static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
// process incoming messages here
switch(msg.what)
{
case EXCHANGE_START: //交换特效开始
{
Bundle b = msg.getData();
int col1 = b.getInt("col1");
int col2 = b.getInt("col2");
int row1 = b.getInt("row1");
int row2 = b.getInt("row2");
mInExchange[col1][row1] = true; //处于交换状态
mInExchange[col2][row2] = true;
drawExchange = new DrawExchange(drawAnimal, mPic[col1][row1], col1, row1, mPic[col2][row2], col2, row2);
control.exchange = drawExchange.control;
break;
}
case EXCHANGE_END: //交换特效结束
{
Bundle b = msg.getData();
int col1 = b.getInt("col1");
int col2 = b.getInt("col2");
int row1 = b.getInt("row1");
int row2 = b.getInt("row2");
int picId = mPic[col1][row1];
mPic[col1][row1] = mPic[col2][row2];
mPic[col2][row2] = picId;
mInExchange[col1][row1] = false; //交换状态解除
mInExchange[col2][row2] = false;
control.exchange = null;
drawExchange = null;
break;
}
case LOADING_START: //加载动作开始
drawLoading = new DrawLoading(loadingTextureId); //创建加载动画素材
control.loading = drawLoading.control;
case LOADING_END: //加载动作结束
control.loading = null;
drawLoading = null;
break;
}
}
};
最后的效果如下,疯狂消除的雏形已经初现了!
用手指在屏幕上滑动,已经可以实现交换的效果了。在下节,我们将完整实现一个游戏用到的全部基本特效。