Package com.example.topnews.topnews;
Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.graphics.PixelFormat;
Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageView;
Import Android.widget.TextView;
/**
* Created by Mr Lei on 2016/6/23.
*/
public class Dayornight extends Fragmentactivity {
Public WindowManager Mwindowmanger;
Set Night mode
Public Sharedpreferences SKINSP;
Public final static String day = ' Day ';
Public final static String night = "Night";
public Boolean isday;
Public View MyView;
Public ImageView imageview_night;
Public TextView text_night;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Mwindowmanger= (WindowManager) Getsystemservice (Window_service);
Init ();
}
Daytime Night Mode switching
public void init () {
SKINSP = getsharedpreferences ("Skinchange", mode_private);
String mode = skinsp.getstring ("Skin", "" ");
if (mode! = NULL | |!mode.equals ("")) {
if (Mode.equals (night)) {
Night ();
Isday = false;
} else {
Day ();
Isday = true;
}
}
}
public void Night () {
@SuppressWarnings ("deprecation")
Windowmanager.layoutparams params = new Windowmanager.layoutparams (
Android.view.ViewGroup.LayoutParams.FILL_PARENT, Android.view.ViewGroup.LayoutParams.FILL_PARENT,
Layoutparams.type_application,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Pixelformat.translucent);
params.gravity = Gravity.center;
Params.y = 10;
if (MyView = = null) {
MyView = new View (this);
Myview.setbackgroundcolor (0x80000000);
}
Mwindowmanger.addview (MyView, params);
Editor edit = Skinsp.edit ();
Edit.putstring ("Skin", night);
Edit.commit ();
}
public void Day () {
if (MyView! = null) {
Mwindowmanger.removeview (MyView);
Editor edit = Skinsp.edit ();
Edit.putstring ("Skin", day);
Edit.commit ();
}
}
@Override
public void OnDestroy () {
Super.ondestroy ();
String mode = skinsp.getstring ("Skin", "" ");
if (Mode.equals (night)) {
Removeskin ();
}
}
public void Removeskin () {
if (MyView! = null) {
Mwindowmanger.removeview (MyView);
}
}
}
-------------------------------------------Implementation------------------------------------------------
String mode = skinsp.getstring ("Skin", "" ");
if (mode! = NULL | |!mode.equals ("")) {
if (Mode.equals (night)) {
Imgmode.setimageresource (R.drawable.mode_night);
Tvmode.settext ("Night Mode");
Day ();
Isday = true;
} else {
Imgmode.setimageresource (R.drawable.mode_day);
Tvmode.settext ("day Mode");
Night ();
Isday = false;
}
}
Simple daytime mode night mode