Intent【源码下载】
一丶代码演示效果:
二丶随堂笔记
不同的形式实现activity之间的切换(对应查照)
//直接查找法 public void componentClick(View v){ //Intent intent = new Intent(); //ComponentName componentName = new ComponentName(this,Main2Activity2.class); //intent.setComponent(componentName); //上面的简写形式 Intent intent = new Intent(this,Main2Activity2.class); startActivity(intent);//启动这个activity }
//通过action查找组件和category属性 public void actionOclick(View v){ Intent intent = new Intent(); intent.setAction("com.My_ACTION"); intent.addCategory("com.MY_CATEGORY"); startActivity(intent); } 程序清单配置:需要配置 intent-filter <activity android:name=".Main2Activity3" android:label="Main2Activity3" > <intent-filter android:priority="2"> <action android:name="com.My_ACTION"></action> <category android:name="android.intent.category.DEFAULT"></category>//必须 <category android:name="com.MY_CATEGORY"></category> </intent-filter> </activity>
//data属性与action配合合使用 public void dateClick(View v){ //打开浏览器,并访问百度首页 //因为这个意图符合浏览器的activity,所以能打开 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri data = Uri.parse("http://www.baidu.com"); intent.setData(data); startActivity(intent); } 清单配置: <activity android:name=".Main2Activity4" android:label="@string/title_activity_main24" > <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" android:host="www.baidu.com"></data> </intent-filter> </activity>
//使用type属性:表示数据类型 public void typeClick(View v){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri data = Uri.parse("http://www.baidu.com"); intent.setData(data); //mime类型 intent.setDataAndType(data,"text/html");//intent.setData(data):不能单独使用,单独使用会将data 自动设置为null startActivity(intent); } <activity android:name=".MainActivity2Activity" android:label="@string/title_activity_main_activity2" > <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:scheme="http" android:host="www.baidu.com" android:mimeType="text/html"></data> </intent-filter> </activity>
查看Activity储存
下面代码实现AB相互跳转,测试各模式各标志,AB进出栈形式
ActivityA类: public void startA(View v){ Intent intent = new Intent(this,MainActivity22ActivityA.class); startActivity(intent); } public void startB(View v){ Intent intent = new Intent(this,MainActivity23ActivityB.class); startActivity(intent); } ActivityB类: public void startA(View v){ Intent intent = new Intent(this,MainActivity22ActivityA.class); startActivity(intent); } public void startB(View v){ Intent intent = new Intent(this,MainActivity23ActivityB.class); startActivity(intent); } 清单配置文件: <activity android:name=".MainActivity23ActivityB" android:label="@string/title_activity_main_activity23_activity_b" android:launchMode="standard" //标准模式,默认的模式,每次启动会新创建一个新的activity > </activity>
和standard模式类似 singleTop:当单击按钮,打开相应的activity时,会在当前任务栈中,判断栈顶是否为当前的Activity,如果是,
就直接使用,如果不是,再创建 新的Activity放入栈顶 <activity android:name=".MainActivity23ActivityB" android:label="@string/title_activity_main_activity23_activity_b" android:launchMode="singleTop" >......(略)
//决定activity应该怎么启动 public void flagClick(View v){ Intent intent = new Intent(this,MainActivity23Activity5.class); //设置activity启动模式 //Intent.FLAG_ACTIVITY_NEW_TASK : 在新的任务栈中启动Activity //Intent.FLAG_ACTIVITY_CLEAR_TOP: 相当于singTop //Intent.FLAG_ACTIVITY_CLEAR_TASK: 相当于singTask intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); }
源码单击事件完成功能
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //打开网页 public void borsePageClick(View v) { Uri data = Uri.parse("http://ww.baidu.com"); String action = Intent.ACTION_VIEW; Intent intent = new Intent(action, data); startActivity(intent); } //打开拨号面板 public void callPhoneDialClick(View v) { Uri data = Uri.parse("tel:123123"); String action = Intent.ACTION_DIAL; Intent inten = new Intent(action, data); startActivity(inten); } //打电话 public void callPhoneClick(View v) { Uri data = Uri.parse("tel:123123"); String action = Intent.ACTION_CALL; Intent inten = new Intent(action, data); startActivity(inten); } //打开发短信的面板 public void smsDialClick(View v) { Intent intent = new Intent(); String action = Intent.ACTION_VIEW; intent.setAction(action); intent.putExtra("sms_body", "你好啊"); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent); } //发短信,带填写号码 public void smsSendClick(View v) { Uri data = Uri.parse("smsto:123456"); String action = Intent.ACTION_SENDTO; Intent intent = new Intent(action,data); intent.putExtra("sms_body","看到短信了吗"); startActivity(intent); } //打开播放器 public void playMusicClick(View v){ Uri data = Uri.parse("file:///sdcard/Music/xpg.map3"); String action = Intent.ACTION_VIEW; Intent intent = new Intent(); intent.setAction(action); intent.setDataAndType(data,"audio/mp3"); startActivity(intent); } //卸载apk程序 public void uninstallClick(View v){ Uri data = Uri.parse("package:comp.coding");//包,就是主清单中的包名 String action = Intent.ACTION_DELETE; Intent intent = new Intent(action,data); startActivity(intent); } //安装apk public void installClick(View v){ Uri data = Uri.fromFile(new File("/sdcard/Download/hello.apk")); String action = Intent.ACTION_VIEW; Intent intent = new Intent(action); intent.setDataAndType(data, "application/vnd.android.package-archive"); startActivity(intent); } }电话功能需要在AndroidManifest.xml添加一句权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>Intent的总结就到这里,希望对你有帮助,关于功能的实现可以留着当时代码积累。