显式意图激活另外一个activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
这段代码的作用是告诉系统在桌面创建一个快捷图标1.布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="click1"
android:text="跳转到第二个界面1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="24dp"
android:onClick="click2"
android:text="跳转到第二个界面2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginLeft="44dp"
android:layout_marginTop="27dp"
android:onClick="click3"
android:text="检查手机网络状态" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button3"
android:layout_marginLeft="16dp"
android:layout_marginTop="37dp"
android:onClick="click4"
android:text="跳转到图库" />
</RelativeLayout>
2.Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View view) {
Intent intent = new Intent();
intent.setClassName(this, "com.itheima.explicitintent.SecondActivity");
this.startActivity(intent);
}
public void click2(View view) {
Intent intent = new Intent(this, SecondActivity.class);
this.startActivity(intent);
}
public void click3(View view) {
ConnectivityManager manager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
Toast.makeText(this, "网络可用", 0).show();
} else {
Toast.makeText(this, "网络不可用", 0).show();
Intent intent = new Intent();
intent.setClassName("com.android.phone",
"com.android.phone.MiuiSettings");
this.startActivity(intent);
}
}
public void click4(View view) {
Intent intent = new Intent();
intent.setClassName("com.miui.gallery", "com.miui.gallery.app.Gallery");
this.startActivity(intent);
}
}
3.授权
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
隐式意图激活另外一个activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
Intent intent =new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
this.startActivity(intent);
}
public void send(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("sms:10086"));
intent.addCategory("android.intent.category.DEFAULT");
this.startActivity(intent);
}
}
隐式意图的配置
第一个Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("com.itheima.secondActivity");
// 额外信息,提供一些执行的环境参数
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.parse("itheima:jerry"),
"vnd.android.cursor.item/mp3");
this.startActivity(intent);
}
}
第二个Activity
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = this.getIntent();
Uri uri = intent.getData();
String data = uri.toString();
String type = intent.getType();
Toast.makeText(this, "data = " + data + " , type = " + type, 0).show();
}
}
清单文件
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.itheima.secondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="itheima" android:mimeType="vnd.android.cursor.item/mp3"/>
</intent-filter>
</activity>
在不同activity之间数据传递
1.第一个Activity布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="人品计算器"
android:textSize="30dp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" >
<requestFocus />
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="click"
android:text="计算" />
</LinearLayout>
2.第一个Activity
public class MainActivity extends Activity {
private EditText et_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_name = (EditText) this.findViewById(R.id.et_name);
}
public void click(View view) {
String name = this.et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "姓名不能为空", 0).show();
return;
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("name", name);
this.startActivity(intent);
}
}
3.第二个Activity布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="计算结果"
android:textSize="30dp" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#bbff0000"
android:text="计算结果"
android:textSize="30dp" />
</LinearLayout>
4.第二个Activity
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView tv_result = (TextView) this.findViewById(R.id.tv_result);
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
Random random = new Random();
int rp = random.nextInt(101);
tv_result.setText(name + "的人品为:" + rp);
}
}