Intent之实现功能导航
来源:互联网 发布:淘宝子账号可以开店吗 编辑:程序博客网 时间:2024/05/02 06:10
Intent之实现功能导航
其实很多诸如跳转到通讯录,拨打电话等功能,Action上都有明确提供了。这里主要想说的是如何跳转到一个第三方的应用,即跳转到微信之类的。下面实例如何跳转到微信
Intent intent = new Intent(); //"com.tencent.mm"和"com.tencent.mm.ui.LauncherUI"可以通过logcat获取 ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(componentName); startActivity(intent);
那么,归根到底,实现的步骤就是:
创建一个Intent对象:Intent intent = new Intent()
创建一个ComponentName对象: ComponentName componentName = new ComponentName(”包名”,”Activity类名”)
实现intent的setComponent方法:intent.setComponent(componentName)
开始跳转:startActivity(intent)
下面直接看代码,先看布局文件代码
<?xml version="1.0" encoding="utf-8"?><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"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_wifi" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="无线" /> <Button android:id="@+id/btn_call" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="电话" /> <Button android:id="@+id/btn_mm" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="微信" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_music" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="音乐" /> <Button android:id="@+id/btn_contacts" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="联系人" /> <Button android:id="@+id/btn_sms" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="短信" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_calendar" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="日历" /> <Button android:id="@+id/btn_news" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="新闻" /> <Button android:id="@+id/btn_weather" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="天气" /> </LinearLayout></LinearLayout>
Activity代码
package com.shake.myaction;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.net.Uri;import android.os.Build;import android.provider.CalendarContract;import android.provider.Contacts;import android.provider.ContactsContract;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); } private void findViews() { findViewById(R.id.btn_wifi).setOnClickListener(this); findViewById(R.id.btn_call).setOnClickListener(this); findViewById(R.id.btn_mm).setOnClickListener(this); findViewById(R.id.btn_music).setOnClickListener(this); findViewById(R.id.btn_contacts).setOnClickListener(this); findViewById(R.id.btn_sms).setOnClickListener(this); findViewById(R.id.btn_calendar).setOnClickListener(this); findViewById(R.id.btn_news).setOnClickListener(this); findViewById(R.id.btn_weather).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { //打开wifi设置 case R.id.btn_wifi: intentWifi(); break; //打开通话记录 case R.id.btn_call: intentCall(); break; //打开微信 case R.id.btn_mm: intentMm(); break; //打开音乐播放器 case R.id.btn_music: intentMusic(); break; //打开通讯录 case R.id.btn_contacts: intentContacts(); break; case R.id.btn_sms: intentSms(); break; //打开日历 case R.id.btn_calendar: intentCalendar(); break; //打开网易新闻 case R.id.btn_news: intentNews(); break; //打开天气 case R.id.btn_weather: intentWeather(); break; } } /** * 打开天气 */ private void intentWeather() { Intent intent = new Intent(); ComponentName cn = new ComponentName("com.miui.weather2", "com.miui.weather2.ActivityWeatherCycle"); intent.setComponent(cn); startActivity(intent); } /** * 打开网易新闻 */ private void intentNews() { Intent intent = new Intent(); ComponentName cn = new ComponentName("com.netease.newsreader.activity", "com.netease.nr.biz.ad.AdActivity"); intent.setComponent(cn); startActivity(intent); } /** * 打开日历 */ private void intentCalendar() { Intent intent =new Intent(); ComponentName componentName =new ComponentName("com.android.calendar","com.android.calendar.LaunchActivity"); intent.setComponent(componentName); startActivity(intent); } /** * 打开短信 */ private void intentSms() { Intent intent = new Intent(); intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList"); startActivity(intent); } /** * 打开通讯录 */ private void intentContacts() { Intent intent =new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Contacts.People.CONTENT_URI); startActivity(intent); } /** * 打开音乐播放器 */ private void intentMusic() { Intent intent = new Intent("android.intent.action.MUSIC_PLAYER"); startActivity(intent); } /** * 打开微信 */ private void intentMm() { Intent intent = new Intent(); //"com.tencent.mm"和"com.tencent.mm.ui.LauncherUI"可以通过logcat获取 ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(componentName); startActivity(intent); } /** * 打开通话记录 */ private void intentCall() { Intent intent = new Intent(); intent.setAction("android.intent.action.CALL_BUTTON"); startActivity(intent); } /** * wifi设置 */ private void intentWifi() { Intent intent = new Intent(); intent.setAction("android.net.wifi.PICK_WIFI_NETWORK"); startActivity(intent); }}
效果:在手机上测试过全部都可以实现,但不同手机效果也不一样的
0 0
- Intent之实现功能导航
- android之ViewPager的使用实现应用的导航功能
- 实现导航Tab栏悬浮功能之改进版
- “fullLoad” app(二)之底部导航功能实现
- 安卓通过经纬度,实现intent调用百度,高德,Google地图客户端实现驾车导航功能
- Intent具体功能实现总结
- React Native之导航器Navigator实现导航功能(一)
- React Native之导航器Navigator实现导航功能(二)
- Intent之提供常用功能
- 分页导航功能实现方法
- 实现导航功能的代码
- jQuery实现导航条功能
- css实现浏览器导航功能
- jQuery实现导航栏功能
- Word2013实现导航的功能
- android之鲜为人知的Intent功能
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- 个人笔记 js 14 js实现点击单元格行跳转链接到指定页面
- 指针和地址学习
- 数据结构学习地址--上海交大acm试点班总教头俞勇老师讲的哦,还有其他课程地址,超全icourse
- poj2362 dfs 剪枝 比水题难的题
- PHP __autoload与spl_autoload
- Intent之实现功能导航
- AlertDialog 用法大全
- 安卓开发——TypedValue.applyDimension()
- #学习笔记#(31)正则表达式全部符号解释
- android程序的断点单步调试步骤
- hdu1811 Rank of Tetris(待解决 拓扑排序)
- STM32Cube MicroXplorer
- 来年春天
- 关于编译MPFR遇到的GMP版本问题