显式
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
隐式:
默认的category必须加上
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.fourmajorcomponents.ACTION_START"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Intent intent = new Intent("com.example.fourmajorcomponents.ACTION_START");
startActivity(intent);
每个intent只能指定一个intent,却可以指定多个category
Intent intent = new Intent("com.example.fourmajorcomponents.ACTION_START");
intent.addCategory("com.example.fourmajorcomponents.second");
startActivity(intent);
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.fourmajorcomponents.ACTION_START"></action>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.fourmajorcomponents.second" />
</intent-filter>
</activity>
隐式intent的更多用法
匹配http,音频,视频
<data android:scheme="http">
<data android:scheme="file" android:mimeType="audio/*"/>
<data android:scheme="file" android:mimeType="video/*"/>
第一句是系统内置的一个action,第二句将一个网址解析为一个Uri对象
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
打电话
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:123456"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent) ;
网友评论