横竖屏切换问题
- App 横竖屏切换的时候会销毁当前的 Activity 然后重新创建一个
- 横竖屏切换时 Activity 生命周期:onPause-> onStop-> onDestory-> onCreate->onStart->onResume
- 现在设置一个按钮和一个 TextView 显示文本,点击按钮后,修改 TextView 默认的文本,然后横竖屏切换,会发现 TextView 文本变回之前的内容了
- activity_main.xml 布局文件内容如下:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:orientation="horizontal"
android:rowCount="2">
<TextView
android:id="@+id/myTextView"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFCCCC"
android:text="0"
android:textSize="50sp" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="回退" />
</GridLayout>
- MainActivity 文件内容如下:
package com.example.administrator.helloworld;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
/**
* 一个应用程序可以有1个或多个活动,而没有任何限制。
* 每个为应用程序所定义的活动都需要在 AndroidManifest.xml 中声明,应用的主活动的意图过滤器标签中需要包含 MAIN 动作和 LAUNCHER 类别
* 如果 MAIN 动作还是 LAUNCHER 类别没有在活动中声明,那么应用程序的图标将不会出现在主屏幕的应用列表中。
*/
public class MainActivity extends AppCompatActivity {
String msg = "Android : ";
/**
* 当活动第一次被创建时调用
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 从项目的 res/layout 中的XML文件加载 UI 组件
* android.util.Log#d(java.lang.String, java.lang.String) 方法用于在 Logcat 窗口中打印日志
*/
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
/** 为 按钮绑定单击事件
* 就和 js 中为某个 id 的标签绑定事件差不多*/
Button button = findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
/**重写点击事件的处理方法onClick()
* 动态修改 TextView 的文本值
* 显示 Toast 信息,这个消息会显示到用户界面上,短暂显示后会消失
* */
@Override
public void onClick(View v) {
TextView textView = findViewById(R.id.myTextView);
textView.setText("Game Over");
Toast.makeText(getApplicationContext(), "你点击了按钮:" + new Date().toString(), Toast.LENGTH_SHORT).show();
}
});
}
/**
* 当活动即将可见时调用
*/
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}
/**
* 当活动可见时调用
*/
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
}
/**
* 当其他活动获得焦点时调用
*/
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}
/**
* 当活动不再可见时调用
*/
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}
/**
* 当活动将被销毁时调用
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}
- 操作步骤如下,显然屏幕由竖屏切换为横屏时,Activity 的数据返回了原始状态了,同时 Logcat 日志信息打印如下:
09-20 06:35:04.290 4776-4776/com.example.administrator.helloworld D/Android :: The onPause() event
The onStop() event
The onDestroy() event
09-20 06:35:04.300 4776-4776/com.example.administrator.helloworld D/Android :: The onCreate() event
The onStart() event
09-20 06:35:04.310 4776-4776/com.example.administrator.helloworld D/Android :: The onResume() event
禁止屏幕横竖屏自动切换
- 在 AndroidManifest.xml 中为<Activity 添加一个属性:android:screenOrientation,可以禁止屏幕横竖屏自动切换,有下述可选值:
- unspecified: 默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向。
- landscape: 横屏显示(宽比高要长)
- portrait: 竖屏显示(高比宽要长)
- user: 用户当前首选的方向
- behind: 和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
- sensor: 由物理的感应器来决定,如用户旋转设备,屏幕会横竖屏切换
- nosensor:忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
- 以上面的代码为例,现在修改 AndroidManifest.xml 文件,为 Activity 添加 android:screenOrientation 属性,值为横屏显示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MainService" />
</application>
</manifest>
- 效果如下所示,无论手机如何转到,屏幕方向都是固定的:
系统常见 Activity
- 系统提供的常见的 Activity 如下:
//1.拨打电话, 给移动客服10086拨打电话,dial 表示拨号盘
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);/**2.发送短信,前缀 smsto 固定,冒号后跟号码
* 如给 10086 发送的短信 smsto:10086
* intent.putExtra("sms_body", "Hello"); sms_body 表示消息主体,值为 消息内容
* 效果就是会打开直接进入到手机的短信发送界面,SENDTO 表示发送到
*/
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);//3.发送彩信(相当于发送带附件的短信),stream 表示流
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hello");
Uri uri = Uri.parse("content://media/external/images/media/23");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);/*** 4.打开浏览器:打开 百度 主页
* 会自动调用系统中默认的浏览器打开 地址
* */
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//5.发送电子邮件:(阉割了Google服务的没戏!!!!)
// 给[email protected]发邮件
Uri uri = Uri.parse("mailto:[email protected]");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
// 给[email protected]发邮件发送内容为“Hello”的邮件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("text/plain");
startActivity(intent);
// 给多人发邮件
Intent intent=new Intent(Intent.ACTION_SEND);
String[] tos = {"[email protected]", "[email protected]"}; // 收件人
String[] ccs = {"[email protected]", "[email protected]"}; // 抄送
String[] bccs = {"[email protected]", "[email protected]"}; // 密送
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("message/rfc822");
startActivity(intent);//6.显示地图:
// 打开Google地图中国北京位置(北纬39.9,东经116.3)
Uri uri = Uri.parse("geo:39.9,116.3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//7.路径规划
// 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4)
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);/**
* 8.多媒体播放,url 前缀为固定写法,file:/// 表示本地文件,后面跟文件路径
* 如下所示播放 mp3 文件,会自动调用默认播放器来播放此 mp3 文件 ,文件必须存在
*/
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///storage/emulated/0/neoRecorder/good01.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);/**
* 多媒体播放,url 前缀为固定写法,file:/// 表示本地文件,后面跟文件路径
* 如下所示播放 mp4 文件,会自动调用默认播放器来播放此 mp4 文件
* 确保文件存在,视频会全屏播放覆盖原界面,播放完成后,会自动再退回原界面
*/
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///storage/emulated/0/tencent/MicroMsg/WeiXin/123.mp4");
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);//获取SD卡下所有音频文件,然后播放第一首=-=
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//9.打开摄像头拍照:
// 打开拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
// 取出照片数据
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");//10.进入联系人页面:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);
- 这里以测试 拨打号码为例,其余的同理,布局文件 activity_main.xml 文件仍然以上面的内容,现在 MainActivity 内容修改如下:
package com.example.administrator.helloworld;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
/**
* 一个应用程序可以有1个或多个活动,而没有任何限制。
* 每个为应用程序所定义的活动都需要在 AndroidManifest.xml 中声明,应用的主活动的意图过滤器标签中需要包含 MAIN 动作和 LAUNCHER 类别
* 如果 MAIN 动作还是 LAUNCHER 类别没有在活动中声明,那么应用程序的图标将不会出现在主屏幕的应用列表中。
*/
public class MainActivity extends AppCompatActivity {
String msg = "Android : ";
/**
* 当活动第一次被创建时调用
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 从项目的 res/layout 中的XML文件加载 UI 组件
* android.util.Log#d(java.lang.String, java.lang.String) 方法用于在 Logcat 窗口中打印日志
*/
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
/** 为 按钮绑定单击事件
* 就和 js 中为某个 id 的标签绑定事件差不多*/
Button button = findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
/**重写点击事件的处理方法onClick()
* 动态修改 TextView 的文本值
* 显示 Toast 信息,这个消息会显示到用户界面上,短暂显示后会消失
* 开启新活动
* */
@Override
public void onClick(View v) {
TextView textView = findViewById(R.id.myTextView);
textView.setText("Game Over");
/**
* 拨打电话:前缀 tel 固定,冒号后接电话号码
* 如给移动客服10086拨打电话:tel:10086
*/
Uri uri = Uri.parse("tel:10010");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
/**启动新活动*/
startActivity(intent);
}
});
}
}