隐式Intent
一、在创建Intent实例时,不直接传入需要调用的Activity的名称。而向其传入一个action
Intent intent = new Intent("com.example.helloworld.THIRD_START");
二、被调用的activity注册时需要添加对应的action。注意这里还需要添加至少一个category。
<intent-filter> <action android:name="com.example.helloworld.THIRD_START"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter>
三、这样就可以启动Intent了。需要注意的是,这里并没有在Intent中添加category。这是因为android.intent.category.DEFAULT是默认被添加的category。
startActivity(intent);
四、如果在intent中添加的category与注册的category不匹配。将会出现异常。
Intent intent = new Intent("com.example.helloworld.THIRD_START");
intent.addCategory("com.example.helloworld.OTHER");
startActivity(intent);
五、使用Intent调用系统自带浏览器浏览网页
@Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); }
六、使用Intent调用打电话界面
Button button1 = findViewById(R.id.button4); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view){ Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); } });