收到ACTION_SCREEN_ON意图后如何加快布局通胀?

时间:2016-05-18 06:00:50

标签: java android layout lockscreen

所以我一直在为Android开发一个锁屏应用程序,我已经完成了大部分工作,并且响应非常快。我现在面临的问题是,每当我关闭应用程序并锁定手机并在下一刻解锁时,Lockscreen活动会很晚或根本不显示。

我使用了不同的锁屏应用来检查这种延迟,并且AcDisplay面临同样的问题。 虽然Next Lockscreen(微软)没有。据我所知,它故意延迟屏幕唤醒直到锁屏活动被充气。

因此,如果有人能告诉我如何消除这种延迟或延迟屏幕唤醒,就像Next一样,这将非常有用。

修改

我尝试在LockscreenActivity上使用android:alwaysRetainTaskState =“true”

LockscreenActivity.java

package com.bikimax.lockscreen;

import java.util.Calendar;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.view.GestureDetectorCompat;
import android.telephony.PhoneStateListener;
import android.support.v4.*;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bikimax.lockscreen.util.DigitalClock;
import com.bikimax.lockscreen.util.TimeMap;

public class LockscreenActivity extends Activity{


Drawable wallpaper;
Calendar cal;
ImageView background;

WindowManager winManager;
RelativeLayout layout;
View aView;
Button button;
private static Handler handler;
Vibrator vibra;
boolean bWallpaperEnabled;
boolean btextClockEnabled;
SharedPreferences SP;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("Daze", "Lockscreen Activity Created");
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_lockscreen);


    SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    boolean bWallpaperEnabled = SP.getBoolean("wallpaperEnable",false);

     WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
             WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
             WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH|
             WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|
             View.SYSTEM_UI_FLAG_LAYOUT_STABLE| 
             View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
             View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
             View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
             View.SYSTEM_UI_FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT);
     winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
     layout = new RelativeLayout(getApplicationContext());
     getWindow().setAttributes(localLayoutParams);


     View.inflate(this, R.layout.activity_lockscreen, this.layout);
     this.winManager.addView(layout, localLayoutParams); 
    // mDetector = new GestureDetector(this,this);



    // myThread = null;
    // Thread myThread = null;
    // Runnable myRunnableThread = new CountDownRunner();
    // myThread = new Thread(myRunnableThread);
    // myThread.start();



     //Fonts
     final Typeface OpenSans = Typeface.createFromAsset(getAssets(), "fonts/OpenSans/OpenSans-Light.ttf"); // size: 27dp
     final Typeface Alcubierre = Typeface.createFromAsset(getAssets(), "fonts/Alcubierre.otf"); // numeral size: 40dp 

     getApplicationContext();
    TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
     TelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE); 

    WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    button = (Button) layout.findViewById(R.id.button1);

    button.setTypeface(OpenSans);
    background = (ImageView) layout.findViewById(R.id.imageView1);
    if(bWallpaperEnabled == true){
       background.setBackground(myWallpaperManager.getDrawable());
       background.getBackground().setColorFilter(0x7f000000, android.graphics.PorterDuff.Mode.DARKEN);
    }



   handler = new Handler();

   Runnable updateCurrentTime = new Runnable(){
        @Override
        public void run() 
        {
            SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            btextClockEnabled = SP.getBoolean("textClockEnable",false);

            cal = Calendar.getInstance();

            String digitalClock = DigitalClock.createTimeString(cal.get(Calendar.MINUTE), cal.get(Calendar.HOUR));
            String textClock = TimeMap.createTimeString(cal.get(Calendar.MINUTE),cal.get(Calendar.HOUR_OF_DAY));


            if(btextClockEnabled == true){
                button.setTypeface(OpenSans);
                button.setText(textClock);

            }

            else{
                button.setTextSize(40);
                button.setTypeface(Alcubierre);
                button.setText(digitalClock);
            } // Update the current Time
            handler.postDelayed(this,1000);  // Run this again in 1 second
            // (i.e. we create an infinite loop that
            // executes every second)
        }
    };

    handler.postDelayed(updateCurrentTime,1); // The first call to the
    // updateCurrentTime Runnable with
    // a 5 second delay   

   Log.i("Daze","Lockscreen Activity Set Up");

}

  public void unlock(){

      finish();
      System.exit(0);


  }

  public void onUnlock(View view){
        Log.i("Daze", "Unlock Button Pressed");
        vibra =  (Vibrator) getApplicationContext().getSystemService(VIBRATOR_SERVICE);
        vibra.vibrate(350);
        unlock();
    }

    @SuppressWarnings("deprecation")
    public void onDestroy()
    {
        winManager.removeView(this.layout);
        layout.removeAllViews();

        super.onDestroy();
        //myThread.interrupt();
        Log.i("Daze","Lockscreen Activity Destroyed");
    }

    /*
    public void doWork(){
        runOnUiThread(new Runnable(){
            public void run(){
                try{

                    //Fonts
                    Typeface OpenSans = Typeface.createFromAsset(getAssets(), "fonts/OpenSans/OpenSans-Light.ttf"); // size: 27dp
                    Typeface Alcubierre = Typeface.createFromAsset(getAssets(), "fonts/Alcubierre.otf"); // numeral size: 40dp 
                    SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    btextClockEnabled = SP.getBoolean("textClockEnable",false);

                    cal = Calendar.getInstance();

                    String digitalClock = DigitalClock.createTimeString(cal.get(cal.MINUTE), cal.get(cal.HOUR));
                    String textClock = TimeMap.createTimeString(cal.get(cal.MINUTE),cal.get(cal.HOUR_OF_DAY));


                    if(btextClockEnabled == true){
                        button.setTypeface(OpenSans);
                        button.setText(textClock);

                    }

                    else{
                        button.setTextSize(40);
                        button.setTypeface(Alcubierre);
                        button.setText(digitalClock);
                    }




                }
                catch(Exception ex){}
            }
        });
    }




    private class CountDownRunner implements Runnable{

        @Override
        public void run(){
            while(!Thread.currentThread().isInterrupted()){
                try {
                    doWork();
                    Thread.sleep(1000);
                }
                catch(InterruptedException e){
                    Thread.currentThread().interrupt();
                }
                catch(Exception e){

                }
            }
        }
    }

    */


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.lockscreen, 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);
}

class TeleListener extends PhoneStateListener {
      public void onCallStateChanged(int state, String incomingNumber) {
       super.onCallStateChanged(state, incomingNumber);
       switch (state) {
       case TelephonyManager.CALL_STATE_RINGING:
        unlock();
        break;
       default:
        break;
       }
      }

     }

}

LockscreenReceiver.java

package com.bikimax.lockscreen;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.WindowManager;

public class LockscreenReceiver extends BroadcastReceiver {
public LockscreenReceiver() {

}

@Override
public void onReceive(Context context, Intent intent0) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    Intent intent = new Intent(context,LockscreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
    context.startActivity(intent);
    Log.i("Daze", "Lockscreen activated");
}  

}

0 个答案:

没有答案