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,数量不限
// 同时Category的name必须有并且是"android.intent.category.DEFAULT"
public void b9_OnClick(View v)
{
Intent intent=
new Intent("android.MAIN1"
);
intent.addCategory("android.MAIN1"
);
startActivity(intent);
}
}
MainActivity
package com.example.wang.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class TestActivity1
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_relativelayout);
//练习findViewById()方法
EditText editText=
(EditText)findViewById(R.id.t2);
editText.setHint("Subject"
);
//意图传递信息,2-接收数据,在MainActivity中 1-发送信息
Intent intent=
getIntent();
String strname=intent.getStringExtra("name"
);
String strname1=intent.getStringExtra("name1"
);
Log.e("TAG","意图传递的值="+
strname);
Log.e("TAG","意图传递的值1="+
strname1);
}
}
TestActivity1
package com.example.wang.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class TestActivity2
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_relativelayout);
//练习findViewById()方法
EditText editText=
(EditText)findViewById(R.id.t2);
editText.setHint("测试2"
);
//意图传递信息,2-接收数据,在MainActivity中 1-发送信息
Intent intent=
getIntent();
String strname=intent.getStringExtra("name"
);
String strname1=intent.getStringExtra("name1"
);
Log.e("TAG","意图传递的值="+
strname);
Log.e("TAG","意图传递的值1="+
strname1);
}
}
TestActivity2
<?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">
<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>
</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">
<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"/>
</LinearLayout>
test_linearlayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"
android:id="@+id/t1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/t1"
android:hint="To"
android:id="@+id/t2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/t2"
android:hint="Message"
android:id="@+id/t3"
android:layout_above="@+id/t4"
android:gravity="top"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/t4"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Resent"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
test_relativelayout
转载于:https://www.cnblogs.com/wangchuanqi/p/5438100.html
相关资源:JAVA上百实例源码以及开源项目