Android Screen Switch Summary _android

Source: Internet
Author: User

The Android phone or tablet will have the ability to switch on and off, usually by physical gravity sensors, but sometimes it's not, and it's usually in the setup that we can turn off the screen switch on the phone.

Androidmanifest.xml

<activity android:name= "com.google.android.gms.ads.AdActivity" android:configchanges= "Keyboard|keyboardhidden |orientation|screenlayout|uimode|screensize|smallestscreensize "Android:theme=" @android: style/Theme.Translucent "/> <?xml version=" 1.0 "encoding=" Utf-8 "?> <manifest xmlns:android=" http://schemas.android.com/apk/res/ Android "package=" Com.example.yanlei.yl7 > <!--Include required permissions for Google Mobile Ads to run. --> <uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission " Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission android:name=" android.permission.CHANGE_ CONFIGURATION "/> <application android:allowbackup=" true "android:icon=" @mipmap/ic_launcher "android:label=" @ String/app_name "android:theme=" @style/apptheme > <!--this meta-data the tag is required to with Google play Services. --> <meta-data android:name= "com.google.android.gms.version" android:value= "@integer/goOgle_play_services_version "/> <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> <!--Include the adactivity configchanges and theme. --> <activity android:name= "com.google.android.gms.ads.AdActivity" android:configchanges= "keyboard|" Keyboardhidden|orientation|screenlayout|uimode|screensize|smallestscreensize "Android:theme=" @android: style/ Theme.translucent "/> </application> </manifest>

Activity_main.xml

<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 "
>
<button
android:id= "@+id/button"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:text= "Get configuration Info"
android:textsize= "25SP"
android: layout_margintop= "80dip"
android:layout_centerhorizontal= "true"
/>
<textview
android : layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= " Test Configurationchange "
android:textsize=" 25sp "
android:layout_centerinparent=" true "
android:id= "@+id/mytext"/>
</RelativeLayout>

Mainactivity.java

Package com.example.yanlei.yl7;
Import android.content.res.Configuration;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast; public class Mainactivity extends Appcompatactivity {private Button Mbutton; private TextView ptextview; @Override Protec Ted void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_
Main);
ptextview= (TextView) This.findviewbyid (R.id.mytext);
SYSTEM.OUT.PRINTLN ("---> onCreate ()");
Init (); private void Init () {Mbutton = (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (new Clicklistenerimpl ()
); Private class Clicklistenerimpl implements Onclicklistener {@Override public void OnClick (View v) {Getconfigurationinf
O (); }} private void Getconfigurationinfo ({Configuration Configuration = getresources (). GetConfiguration ();//get screen direction int l = Configuration.
Orientation_landscape; int p = Configuration.
orientation_portrait;
if (configuration.orientation = = L) {ptextview.settext ("now is horizontal screen = =");
System.out.println ("Now is horizontal screen");
} if (configuration.orientation = = p) {Ptextview.settext ("Now is the vertical screen = = =");
System.out.println ("Now is the vertical screen"); @Override public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig);// Newconfig.orientation to obtain the current screen state is horizontal or vertical//configuration.orientation_portrait indicating vertical//configuration.orientation_
Landscape represents a horizontal screen if (newconfig.orientation==configuration.orientation_portrait) {Ptextview.settext ("Now is a vertical screen");
Toast.maketext (Mainactivity.this, "Now is the vertical screen", toast.length_short). Show ();
} if (Newconfig.orientation==configuration.orientation_landscape) {Ptextview.settext ("now horizontal screen");
Toast.maketext (Mainactivity.this, "Now is horizontal screen", Toast.length_short). Show (); }} @Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); outstate.putstring ("Name", "Zxx"); Outstate.putint ("id", 9527);
SYSTEM.OUT.PRINTLN ("---> onsaveinstancestate ()"); } @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate (
Savedinstancestate);
String name = savedinstancestate.getstring ("name");
int id = savedinstancestate.getint ("id");
SYSTEM.OUT.PRINTLN ("---> onrestoreinstancestate ()");

System.out.println ("name =" + name + ", Number =" + ID); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds the items to the action bar if it I
s present.
Getmenuinflater (). Inflate (R.menu.menu_main, menu);
return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long//as your specify a parent activity in a
ndroidmanifest.xml. int id = item.getitemid (); if (id = = r.id.action_settings) {return TruE
return super.onoptionsitemselected (item); }
}

The above content to introduce you to the Android screen switch summary of the relevant knowledge, hope to help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.