显示Intent
一 构建过程
1、 首先添加一个名为FirstActivity的类继承自Activity,在Layout下创建名为first_layout的LinearLayout线性布局文件,并添加一个按钮。最后在AndroidManifest.xml 文件下进行注册(假如创建工程时没有创建活动,这时在AndroidManifest.xml 中添加活动记得要加<intent-filter>
里面的action和category)。src下面的包要和gen下面的名称一样
2、使用相同的方法构建SecondActivity的类等文件 注册AndroidManifest.xml时可以不添加<intent-filter>
二 实例
//FirstActivity
package com.example.learnintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class FirstActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this,SecondActivity.class));
//这里很明确的指出就是要访问SecondAcitivity的活动
}
});
}
}
// SecondActivity
package com.example.learnintent;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
// first_layout 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
//second_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第二个activity" />
</LinearLayout>
//AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.learnintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="This is a Firstactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity">
</activity>
</application>
</manifest>
**
隐式Intent
**
一 叙述
隐式Intent和显示Intent的区别是不是明确启动那个活动由系统判断去启动哪个活动,需要用到<intent-filter>
的属性
二 实例
1、方式一 在AndroidManifest.xml下配置<intent-filter>
//AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.learnintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="This is a Firstactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.learnintent.intent.action.SecondActivity" />//匹配可以是任意的字符串
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
//FirstActivity
package com.example.learnintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class FirstActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent("com.example.learnintent.intent.action.SecondActivity"));
}
});
}
}
修改后与第一个运行效果是相同的
2、启动另一个应用的Activity 因为在A应用里是无法获取到B应用里的Activity类的定义的
再新建一个android程序
在布局中添加一个按钮
//新android程序 IntentTest
package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent("com.example.learnintent.intent.action.SecondActivity"));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public 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.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//原FirstActivity 修改为
package com.example.learnintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class FirstActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondActivity.ACTION));
//注意这里改为ACTION 在SecondActivity定义
}
});
}
}
//原SecondActivity
package com.example.learnintent;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
public static final String ACTION =
“com.example.learnintent.intent.action.SecondActivity” ;
//改定义
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
“`
这时 这两个应用都可以访问到IntentActivity内的SecondActivity活动
如果想IntentActivity不想让IntentTest应访问可以在AndroidManifest.xml
修改为<activity
android:name=".SecondActivity" android:exported="false">**重点内容**