Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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_Kiosk Mode - Fatal编程技术网

Android 信息亭模式不工作。?

Android 信息亭模式不工作。?,android,kiosk-mode,Android,Kiosk Mode,我正在我的应用程序中创建带有kiosk模式的示例应用程序,以检查kiosk模式是否有效。?。但示例应用程序中的kiosk模式不起作用。我的意思是在屏幕关闭后,活动不会开始 我从[ 这是我的主要活动 public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在我的应用程序中创建带有kiosk模式的示例应用程序,以检查kiosk模式是否有效。?。但示例应用程序中的kiosk模式不起作用。我的意思是在屏幕关闭后,活动不会开始

我从[

这是我的主要活动

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    setContentView(R.layout.activity_main);
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@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);
   }
 }
}
这是我的申请课

public class AppContext extends Application {

  private AppContext instance;
  private PowerManager.WakeLock wakeLock;
  private OnScreenOffReceiver onScreenOffReceiver;
  PowerManager pm;

  @Override
  public void onCreate() {
    super.onCreate();
    instance = this;
    registerKioskModeScreenOffReceiver();
     pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
     startKioskService();
  }

  private void registerKioskModeScreenOffReceiver() {
    // register screen off receiver
    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    onScreenOffReceiver = new OnScreenOffReceiver();
    registerReceiver(onScreenOffReceiver, filter);
  }

  public PowerManager.WakeLock getWakeLock() {
    if(wakeLock == null) {
      // lazy loading: first call, create wakeLock via PowerManager.

      wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakeup");
    }
    return wakeLock;
  }
  private void startKioskService() { // ... and this method
      startService(new Intent(this, KioskService.class));
    }
}
这是接收级

public class OnScreenOffReceiver extends BroadcastReceiver {
  private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";

  @Override
  public void onReceive(Context context, Intent intent) {
    if(Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){
      AppContext ctx = (AppContext) context.getApplicationContext();
      // is Kiosk Mode active?
      if(isKioskModeActive(ctx)) {
        wakeUpDevice(ctx);
      }
    }
  }

  private void wakeUpDevice(AppContext context) {
    PowerManager.WakeLock wakeLock = context.getWakeLock(); // get WakeLock reference via AppContext
    if (wakeLock.isHeld()) {
      wakeLock.release(); // release old wake lock
    }

    // create a new wake lock...
    wakeLock.acquire();

    // ... and release again
    wakeLock.release();
  }

  private boolean isKioskModeActive(final Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    return sp.getBoolean(PREF_KIOSK_MODE, false);
  }


}
这是服务舱

public class KioskService extends Service {

  private static final long INTERVAL = TimeUnit.SECONDS.toMillis(2); // periodic interval to check in seconds -> 2 seconds
  private static final String TAG = KioskService.class.getSimpleName();
  private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";

  private Thread t = null;
  private Context ctx = null;
  private boolean running = false;

  @Override
  public void onDestroy() {
    Log.i(TAG, "Stopping service 'KioskService'");
    running =false;
    super.onDestroy();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Starting service 'KioskService'");
    running = true;
    ctx = this;

    // start a thread that periodically checks if your app is in the foreground
    t = new Thread(new Runnable() {
      @Override
      public void run() {
        do {
          handleKioskMode();
          try {
            Thread.sleep(INTERVAL);
          } catch (InterruptedException e) {
            Log.i(TAG, "Thread interrupted: 'KioskService'");
          }
        }while(running);
        stopSelf();
      }
    });

    t.start();
    return Service.START_NOT_STICKY;
  }

  private void handleKioskMode() {
    // is Kiosk Mode active? 
      if(isKioskModeActive(ctx)) {
        // is App in background?
      if(isInBackground()) {
        restoreApp(); // restore!
      }
    }
  }

  private boolean isInBackground() {
    ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;
    return (!ctx.getApplicationContext().getPackageName().equals(componentInfo.getPackageName()));
  }

  private void restoreApp() {
    // Restart activity
    Intent i = new Intent(ctx, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ctx.startActivity(i);
  }

  public boolean isKioskModeActive(final Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    return sp.getBoolean(PREF_KIOSK_MODE, false);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}
公共类KioskService扩展服务{
private static final long INTERVAL=TimeUnit.SECONDS.toMillis(2);//签入的周期间隔秒->2秒
私有静态最终字符串标记=KioskService.class.getSimpleName();
私有静态最终字符串PREF_KIOSK_MODE=“PREF_KIOSK_MODE”;
私有线程t=null;
私有上下文ctx=null;
私有布尔运行=false;
@凌驾
公共空间{
Log.i(标记“停止服务‘KioskService’”);
运行=错误;
super.ondestory();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.i(标签,“启动服务‘KioskService’”);
运行=真;
ctx=这个;
//启动一个线程,定期检查你的应用程序是否在前台
t=新线程(新可运行(){
@凌驾
公开募捐{
做{
handleKioskMode();
试一试{
睡眠(间隔);
}捕捉(中断异常e){
i(标记“线程中断:'KioskService'”);
}
}跑步时;
stopSelf();
}
});
t、 start();
退货服务。开始时不粘;
}
私有void handleKioskMode(){
//信息亭模式是否处于活动状态?
如果(isKioskModeActive(ctx)){
//应用程序在后台吗?
if(isInBackground()){
restoreApp();//还原!
}
}
}
私有布尔值isInBackground(){
ActivityManager am=(ActivityManager)ctx.getSystemService(Context.ACTIVITY_服务);
List taskInfo=am.getRunningTasks(1);
ComponentName componentInfo=taskInfo.get(0).topActivity;
返回(!ctx.getApplicationContext().getPackageName().equals(componentInfo.getPackageName());
}
私有无效恢复{
//重新启动活动
意向i=新意向(ctx,MainActivity.class);
i、 addFlags(意图、标志、活动、新任务);
星触觉(i);
}
公共布尔值isKioskModeActive(最终上下文){
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(上下文);
返回sp.getBoolean(PREF_KIOSK_模式,false);
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
}
这是我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kioskmode"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:name=".AppContext"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>
    <service android:name="com.example.kioskmode.KioskService" android:exported="false"></service>
    <receiver android:name="com.example.kioskmode.OnScreenOffReceiver" android:enabled="true" >
        <intent-filter android:priority="1000" >
            <action android:name="android.app.action.SCREEN_OFF" />
            <action android:name="android.app.action.SCREEN_ON" />
        </intent-filter>
    </receiver>        
</application>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission       android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

</manifest>


任何人都可以解决这个问题…对我来说..提前谢谢..

你的方法
isKioskModeActive
总是错误的。因此它不会还原你的应用程序。

你的方法
isKioskModeActive
总是错误的。因此它不会还原你的应用程序