2015-08-09 9 views
1

Я создаю пример приложения с режимом киоска в своем приложении, чтобы проверить работу киоска или нет. Но в образцовом режиме киоск приложения не работает. Я имею в виду, что после выключения экрана активность не начинается.Режим киоска не работает.?

Я взял это из [http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/][here]

Это моя основная деятельность

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

Это мой класс Application

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

Это мой манифест

<?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> 

Любой может решить эту проблему ... для меня .. ?? Спасибо заранее.

ответ

0

Ваш метод isKioskModeActive всегда неверен. Таким образом, он не восстановит ваше приложение.