Today the group chat said is to realize the daytime night mode switch not to engage, online looked at a bit of theme set. Think about it, the daytime night mode switch is nothing more than re-set the background color of the space and font color, if so, with broadcast receiver is not a good way to achieve it?
On the code:
Create a new project Dayandnightdemo. Open Fragment_main.xml to add a button.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.dayandnightdemo.mainactivity$placeholderfragment" > <textview android:id= "@+id/main_h Ello "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text=" @string/ Hello_world "/> <button android:id=" @+id/main_btn "android:layout_width=" Match_parent "Andro id:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:text= "@string/jump"/></R Elativelayout>
Open Res/menu/main.xml Add a menu
<menu xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/ Apk/res-auto " xmlns:tools=" Http://schemas.android.com/tools " tools:context=" Com.example.dayandnightdemo.MainActivity "> <item android:id=" @+id/action_settings " android:o rderincategory= "android:title=" @string/action_settings " app:showasaction=" never "/> < Item android:id= "@+id/action_change" android:orderincategory= " app:showasaction=" Never "/>" </menu>
To Mainactivity
Package Com.example.dayandnightdemo;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;import Android.content.intentfilter;import Android.graphics.color;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v7.app.actionbaractivity;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import android.widget.textview;/** * (compared with the new ADT), the bottom will have a demo link, import your own project when you remember to import your own appcompat_v7, * The specific method is the right mouse button dayandnightdemo--"properties--" android--"library--" remove--"Add. * * @author Administrator * */public class Mainactivity extends Actionbaractivity {private static MenuItem Changeitem;@o verrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); if (savedinstancestate = = null) {GETSUPPORTFRAgmentmanager (). BeginTransaction (). Add (R.id.container, New Placeholderfragment ()). commit ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); Changeitem = Menu.finditem (R.id.action_change); if (! Util.flag)//night Changeitem.settitle (R.string.action_change_day); Elsechangeitem.settitle (R.string.action_change_ Night); return true;} @Overridepublic 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 and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} else if (id = = R.id.action_chang e) {Intent Intent = new Intent (), if (Item.gettitle (). toString (). Equals (GetString (r.string.action_change_night))) Util.flag = false;//Night Else if (Item.gettitle (). toString (). Equals (GetString (R.string.action_chaNge_day)) Util.flag = true;//daytime intent.setaction (Util.filter); This.sendbroadcast (intent); return true;} return super.onoptionsitemselected (item);} /** * A placeholder fragment containing a simple view. */public Static class Placeholderfragment extends Fragment {View rootview; TextView Hellotext; Button button; Broadcastreceiver receiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {//T ODO auto-generated method Stubif (! Util.flag) {rootview.setbackgroundcolor (color.black); Hellotext.setbackgroundcolor (Color.BLACK); Hellotext.settextcolor (Color.White); Button.setbackgroundcolor (Color.dkgray); Button.settextcolor (Color.WHITE); MainActivity.changeItem.setTitle (r.string.action_change_day);} else if (Util.flag) {rootview.setbackgroundcolor (color.white); Hellotext.setbackgroundcolor (Color.WHITE); Hellotext.settextcolor (Color.Black); Button.setbackgroundcolor (Color.gray); Button.settextcolor (Color.BLACK); MainActivity.changeItem.setTitle (R.string.action_change_nigHT);}}; Public placeholderfragment () {} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {Rootview = Inflater.inflate (R.layout.fragment_main, container,false); helloText = (TextView) Rootview.findviewbyid (r.id.main_hello); button = (button) Rootview.findviewbyid (R.ID.MAIN_BTN); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubintent Intent = new Intent (getactivity (), Secondactivity.class), Getactivity (). StartActivity (intent);}); I Ntentfilter filter = new Intentfilter (); filter.addaction (Util.filter); Getactivity (). Registerreceiver (Receiver, filter); return Rootview;} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Getactivity (). Unregisterreceiver (receiver); Android.os.Process.killProcess (Android.os.Process.myPid ());}}}
Add a new activity, named Secondactivity
Package Com.example.dayandnightdemo;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;import Android.content.intentfilter;import Android.graphics.color;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.menu;import Android.view.menuitem;import Android.widget.linearlayout;import Android.widget.textview;public Class Secondactivity extends Actionbaractivity {private linearlayout linear;private TextView text;private MenuItem Changeitem ; @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is P Resent.getmenuinflater (). Inflate (R.menu.main, menu); Changeitem = Menu.finditem (R.id.action_change); if (! Util.flag)//night Changeitem.settitle (R.string.action_change_day); Elsechangeitem.settitle (R.string.action_change_ Night); return true;} @Overridepublic 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.x Ml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} else if (id = = r.id.action_change) {Intent int ent = new Intent (); if (Item.gettitle (). toString (). Equals (GetString (r.string.action_change_night)) Util.flag = False ;//night Else if (Item.gettitle (). toString (). Equals (GetString (r.string.action_change_day))) Util.flag = true;// Daytime intent.setaction (Util.filter); This.sendbroadcast (intent); return true;} return super.onoptionsitemselected (item);} @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_second); init (); Intentfilter filter = new Intentfilter (); Filter.addaction (Util.filter); Registerreceiver (receiver, filter);} private void Init () {//TODO auto-generated method stublinear = (linearlayout) Findviewbyid (r.id.second_linear); text = (Te Xtview) findViewbyid (R.id.second_text); if (! Util.flag)//night {Linear.setbackgroundcolor (color.black); Text.setbackgroundcolor (Color.Black); Text.setTextColor ( Color.White);}} Broadcastreceiver receiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {//T ODO auto-generated method Stubif (! Util.flag) {linear.setbackgroundcolor (color.black); Text.setbackgroundcolor (Color.Black); Text.setTextColor ( Color.White); Changeitem.settitle (r.string.action_change_day);} else if (Util.flag) {linear.setbackgroundcolor (color.white); Text.setbackgroundcolor (Color.White); Text.settextcolor (Color.Black); Changeitem.settitle (r.string.action_change_night);}}; @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Unregisterreceiver (receiver) ;}}
Register in Mainifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.dayandnightdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses -SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowba" Ckup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@sty Le/apptheme "> <activity android:name=" com.example.dayandnightdemo.MainActivity "Androi D:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.ac tion. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= "com.example.dayandnightdemo.SecondActivity"/> < /application></manifest>
Util
Package Com.example.dayandnightdemo;public class Util {public static String filter = " Com.example.dayandnightdemo.change ";p ublic static Boolean flag = true;//true for daylight mode, False for Night mode}
Strings.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >dayandnightdemo </string> <string name= "Hello_world" >hello world!</string> <string name= "Action_ Settings ">Settings</string> <string name=" Action_change_night "> Night </string> < String name= "Action_change_day" > Daytime </string> <string name= "jumping" > Jump </string> < String name= "Second_activity" > This is a second activity</string></resources>
Source download (with the comparison of new ADT), the bottom will have a demo link, import your own project when you remember to import your own appcompat_v7, the specific method is the right mouse button dayandnightdemo--"properties--" android--" library--"remove--" Add.
Android Daytime night mode switch implementation