package com.example.wang.myapplication;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_linearlayout);
}
//显式意图方法1
public void b1_OnClick(View v)
{
Intent intent=
new Intent(
this,TestActivity1.
class);
startActivity(intent);
}
//显式意图方法2
public void b2_OnClick(View v)
{
Intent intent=
new Intent();
ComponentName componentName=
new ComponentName(
this,TestActivity1.
class);
intent.setComponent(componentName);
startActivity(intent);
}
//意图传递信息,1-发送信息,在TestActivity中 2-接收数据
public void b3_OnClick(View v)
{
Intent intent=
new Intent(
this,TestActivity1.
class);
intent.putExtra("name","意图传递的值"
);
intent.putExtra("name1", "意图传递的值1"
);
startActivity(intent);
}
//隐式意图方式1,调用系统自身的
public void b4_OnClick(View v)
{
Intent intent=
new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
//隐式意图方式1.1,原理同方式1,调用系统自身的
//在Intent.java中 public static final String ACTION_DIAL = "android.intent.action.DIAL"
public void b41_OnClick(View v)
{
Intent intent=
new Intent("android.intent.action.DIAL"
);
startActivity(intent);
}
//隐式意图方式2,方式1的演变
public void b5_OnClick(View v)
{
Intent intent=
new Intent();
//intent.setAction("android.intent.action.DIAL")和intent.setAction(Intent.ACTION_DIAL)一样
//intent.setAction("android.intent.action.DIAL");
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
}
//隐式意图用date属性实现直接拨打电话功能
//注意事项:当牵扯到费用问题的需要在功能清单文件中声明一下,
//用<uses-permission android:name="android.permission.*"/>
//然后在模拟器相应的应用中设置权限
public void b6_OnClick(View v) {
Intent intent =
new Intent(Intent.ACTION_CALL);
Uri uri = Uri.parse("tel:110"
);
intent.setData(uri);
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
//Category属性实现打开桌面
public void b7_OnClick(View v)
{
//方式1
Intent intent=
new Intent(Intent.ACTION_MAIN);
//有Category语句可以直接打开桌面,如果没有需要选择打开方式
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
//隐式意图打开Activity
//隐式意图利用功能清单文件中action名打开Activity,action名是自己定义的
//情况1:action名独一无二时,直接打开
//情况2:功能清单文件中action名重名的情况,TestActivity1和TestActivity2中action名一样,
// 此时会提示选择哪一个
// 同时Category的name必须有并且是"android.intent.category.DEFAULT"
public void b8_OnClick(View v)
{
Intent intent=
new Intent("android.MAIN1"
);
startActivity(intent);
}
//为解决action重名可以在功能清单文件里添加Category,也可以添加action 数量不限
// 同时Category的name必须有并且是"android.intent.category.DEFAULT"
public void b9_OnClick(View v)
{
Intent intent=
new Intent("android.MAIN1"
);
intent.addCategory("android.MAIN1"
);
startActivity(intent);
}
//测试加载模式
public void b10_OnClick(View v)
{
Intent intent=
new Intent(
this,TestActivity3.
class);
startActivity(intent);
}
//加载模式打开自己
public void b11_OnClick(View v)
{
Intent intent=
new Intent(
this,MainActivity.
class);
startActivity(intent);
}
}
MainActivity
package com.example.wang.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class TestActivity3
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
TextView textView=
(TextView)findViewById(R.id.tv_1);
textView.setText("这是窗口1"
);
}
//用来区分普通模式和单例顶部模式
public void b1_OnClick(View v)
{
Intent intent=
new Intent(
this,MainActivity.
class);
startActivity(intent);
}
}
TestActivity3
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:launchMode="standard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestActivity1">
<intent-filter>
<action android:name="android.MAIN1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".TestActivity2">
<intent-filter>
<action android:name="android.MAIN1" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.MAIN1"/>
</intent-filter>
</activity>
<activity android:name=".TestActivity3"></activity>
</application>
</manifest>
功能清单文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开新的Activity"
android:id="@+id/tv_2"
android:onClick="b1_OnClick"
/>
</LinearLayout>
test_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显式意图方式1"
android:id="@+id/b1_button"
android:onClick="b1_OnClick"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显式意图方式2"
android:onClick="b2_OnClick"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="意图携带数据"
android:onClick="b3_OnClick"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐式意图形式1"
android:onClick="b4_OnClick"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐式意图形式1.1"
android:onClick="b41_OnClick"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐式意图形式2"
android:onClick="b5_OnClick"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐式意图用date属性实现直接拨打电话功能"
android:onClick="b6_OnClick"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Category属性实现打开桌面"
android:onClick="b7_OnClick"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐式意图利用功能清单文件中action名打开Activity"
android:onClick="b8_OnClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="为解决action重名可以添加Category"
android:onClick="b9_OnClick"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="验证加载模式"
android:background="#F00"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试加载模式"
android:onClick="b10_OnClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载模式打开自己"
android:onClick="b11_OnClick"/>
</LinearLayout>
test_linearlayout
切换加载模式如下图更改功能清单文件里的launchMode
1.标准模式 standard
打开步骤:1.1
1.2点击测试加载模式按钮
1.3
1.4点击打开新的ACTIVITY按钮
1.5
1.6点击测试加载模式按钮
1.7
后退步骤:前进步骤倒过来就可。
2.任务栈单例模式 singleTask
2.1
2.2点击测试加载模式按钮
2.3
2.4点击打开新的ACTIVITY按钮
2.5
2.6点击测试加载模式按钮
2.7
后退方式:
2.1.1
2.2.2
2.2.3
3.任务栈顶部单例模式 singleTop
前进与后退步骤都与标准模式相同,不同点是:当MainActivity页面处于最顶端时,
点击加载模式打开自己按钮,任务栈顶部单例模式不再加载,而标准模式重新加载新的页面
4.全局单例模式 singleInstance
4.1
4.2点击测试加载模式
4.3
4.4点击打开新的ACTIVITY按钮
后退
4.1
4.2
4.3
转载于:https://www.cnblogs.com/wangchuanqi/p/5439187.html